Why Does My Submit Button Renders A Page That Is Blank When It Is Supposed To Contain The Data That Was Just Updated?
I'm trying to update the values of my database using a HTML Form. When I Click Edit it brings me to the edit the values above. However as I am clicking the submit button, it retur
Solution 1:
It may not resolve all your problems but it will be more readable as answer.
When you get data from HTML then you create new object SaveClaimForm
and it will have new ID
and you will have the same object in two rows.
You have to get original Claim
from database and update values in this object and save it - and then it will save it with original ID
and you will have only one `object in database
defeditclaims(request,id):
context = initialize_context(request)
user = context['user']
# get original object
claims = SaveClaimForm.objects.get(id=id)
if request.method == 'POST':
# update original object
claims.name = request.POST['name']
claims.email = request.POST['email']
claims.claim = request.POST['claim']
claims.claimtype = request.POST.get('claimtype')
claims.description = request.POST['description']
claims.receipt = request.FILES['receipt']
claims.cheque = request.POST.get('Cheque')
# save it with original `ID`
claims.save()
return render(request, "Login/editclaims.html", {'claims':claims, 'user':user})
BTW:
Django
has special class ModelForm
to create forms in HTML. It may also have methods to check if data in HTML are correct - ie. if fields are not empty, if email is correctly constructed (name@domain.com
), if phone has only numbers, etc. So using ModelForm
can be more useful then writing all manually in code.
Post a Comment for "Why Does My Submit Button Renders A Page That Is Blank When It Is Supposed To Contain The Data That Was Just Updated?"