I am new to the Django framework. I have developed a page with a table and Add button. When the Add button clicks, it will open the form inside a modal. Here the problem is when I first tried, the data in the form filled to the table as expected. But after, when I did the same thing, it gave 200 for POST, and nothing filled in the table. And no errors showing.
This is my sample code.
<!--modal for Create record-->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<form method="POST" class="modal-dialog modal-md" action=".">{% csrf_token %}
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel"><strong>Add Company</strong></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="form-group row">
<label for="companyName" class="col-12 col-md-4"><strong>Company Name</strong></label>
<div class="col-12 col-md-10">
<input type="text" class="form-control" id="companyName" name="companyName" placeholder="Company Name">
</div>
</div>
<div class="form-group row">
<label for="address" class="col-12 col-md-4"><strong>Address</strong></label>
<div class="col-12 col-md-10">
<input type="text" class="form-control" id="address" name="address" placeholder="Address">
</div>
</div>
<div class="modal-footer">
<button type="reset" class="btn btn-secondary">Reset</button>
<button type="submit" class="btn btn-primary">Add</button>
<button type="close" class="btn btn-light" data-dismiss="modal">Close</button>
</div>
</div>
</form>
</div>
And here is the code in views.py
def addcompany(request):
if request.method == 'POST':
if request.POST.get('companyName') and request.POST.get('address'):
saverecord = company()
saverecord.CompanyName = request.POST.get('company')
saverecord.Address = request.POST.get('address')
saverecord.save()
return render(request, 'newapp/companypage.html')
else:
return render(request, 'newapp/companypage.html')
I would be very grateful if anyone can help me to solve this problem. Thank you.
I think the error in the company name you should write it like this:
saverecord.CompanyName = request.POST.get('companyName')
You can also read more about how to build forms from models with Django