This all works fine but what i want to do is have the name of the file i was uploading stored in a hidden input type so when i submit my form with text fields,the name of the image can be submitted to the database.
I have noticed that when i submit my main form to database, the image has been saved into the database. I am able to save it in a temporary folder named "media". However, whenever I edit the form, the image cannot be retrieve anymore
My question is how do i pass data of the uploaded file to my other form?
This is my newclaim.html (Form which submit the receipt)
<form action="/newclaim/" method="post" enctype="multipart/form-data">
<div>
<input id="receipt" type="file" name="receipt_field">
</div>
</form>
This is my existingclaims.html (Form which retrieve the receipt)
<form method="POST" action="/editclaims/{{claims.id}}" enctype="multipart/form-data">
<div>
<input id="receipt" type="file" value="receipt" hidden>
<label for="receipt"> {{ claims.receipt }} </label>
</div>
</form>
This is my views.py
# Submit a new Claim
def newclaim(request):
context = initialize_context(request)
user = context['user']
if request.method == 'POST':
receipt = request.FILES['receipt_field']
ins.save()
return render(request, 'Login/newclaim.html/', {'user':user})
# Edit a claim
def editclaims(request,id):
context = initialize_context(request)
user = context['user']
# get original object
claims = SaveClaimForm.objects.get(id=id)
if request.method == 'POST':
claims.receipt = request.FILES.get('receipt')
# save it with original `ID`
claims.save()
return render(request, "Login/editclaims.html", {'claims':claims, 'user':user})