0

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})
Elton Tan
  • 49
  • 1
  • 13
  • 1
    Does this answer your question? [How to set a value to a file input in HTML?](https://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) – Abdul Aziz Barkat May 28 '21 at 06:53
  • You cannot do that (security reasons). Instead try following the approach Django takes with a checkbox that show the current uploaded file name and allows the user to clear the file if they want to. – Abdul Aziz Barkat May 28 '21 at 06:54
  • What do you mean? – Elton Tan May 28 '21 at 08:04

0 Answers0