'Enable post of HTML form to Django database backend
I'm trying to write the code to enable form data to be sent to the backend database.
The form is just a few fields, name, website, and portfolio address created in html
<div class="card-body">
<form class="needs-validation" form action="/elements/forms/add_investor/" novalidate method="post">
<div class="row">
<div class="col-md-6">
<div class="mb-3">
<label class="form-label" for="validationCustom01">Investor Name</label>
<input type="text" class="form-control" id="validationCustom01" placeholder="InvestorName" value="MyInvestor" required>
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="col-md-6">
<div class="mb-3">
<label for="example-url-input" class="form-label">Investor Website</label>
<input class="form-control" type="url" value="https://example.com" id="example-url-input" requried>
<div class="valid-feedback">
Looks good!
</div>
<div class="invalid-feedback">
Please provide a valid web address.
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="mb-3">
<label for="example-url-input" class="form-label">Investor Portfolio</label>
<input class="form-control" type="url" value="https://example.com/portfolio" id="example-url-input" requried>
<div class="invalid-feedback">
Please provide a valid web address.
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="mb-3">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Comments</h4>
<p class="card-title-desc">Please add any comments or notes</p>
</div>
<div class="card-body">
<div id="ckeditor-classic"></div><br>
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</div>
</div>
</form> <!-- end col -->
</div>
I'm struggling to get understand how I map the fields to the database and then get the submit button posted to the database.
I've gone through the Django forms section, but can't map that to this due to the different elements being used in the HTML
I'm pretty sure I need to create this in the forms.py
, urls.py
, and the models.py
but when I do this if get errors.
So I have started fresh and was hoping someone could show me what I need to map, even if it was just 1 field and submit, then I can understand and do the rest?
Solution 1:[1]
Instead of
<form class="needs-validation" form action="/elements/forms/add_investor/" novalidate method="post">
Use
<form class="needs-validation" action="whatever_the_function_name_is_in_your views.py_file" novalidate method="post">
Also, give your fields proper names, eg.<input class="form-control" type="url" value="https://example.com/portfolio" id="example-url-input" requried name=Investor_portfolio >
this will help each fields fill the right cells in the spreadsheet of the database
watch this video (User Registration in Django (Part-1) – 02:25:55) for a better understanding
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Olujimi David |