1
votes

When I try to post a Django form containing a file field, the file field is being passed as part of the request.POST rather than request.FILES (which is empty). This throws a MultiValueDictKeyError on submitting the form.

Form html

<form class="form-horizontal" action="{% url 'drinkConf' %}" method="post" enctype="multipart/form-data" >

    {% csrf_token %}

  
  <!-- Text input-->

    <label class="col-md-4 control-label" for="date">Date</label>  
    <div class="col-md-4">
    <input id="date" name="date" type="text" placeholder="" class="form-control input-md" required="">
      
    </div>

  
  <!-- Textarea -->

    <label class="col-md-4 control-label" for="notes">Notes</label>
    <div class="col-md-4">                     
      <textarea class="form-control" id="notes" name="notes"></textarea>
    </div>

  <!-- Multiple Radios (inline) -->

    <label class="col-md-4 control-label" for="rating">Rating</label>
    <div class="col-md-4"> 
      <label class="radio-inline" for="rating-0">
        <input type="radio" name="rating" id="rating-0" value=1 checked="checked">
        1
      </label> 
      <label class="radio-inline" for="rating-1">
        <input type="radio" name="rating" id="rating-1" value=2>
        2
      </label> 
      <label class="radio-inline" for="rating-2">
        <input type="radio" name="rating" id="rating-2" value=3>
        3
      </label> 
      <label class="radio-inline" for="rating-3">
        <input type="radio" name="rating" id="rating-3" value=4>
        4
      </label> 
      <label class="radio-inline" for="rating-4">
        <input type="radio" name="rating" id="rating-4" value=5>
        5
      </label>
    </div>



  <label class="form-label" for="image">Upload an image</label>
  <input type="file" class="form-control" id="image" name="image" />


  <!-- Button (Double) -->

    <label class="col-md-4 control-label" for="Submit"></label>
    <div class="col-md-8">
      <input class="btn btn-success" type="submit" value="Submit" />
      <button id="Cancel" name="Cancel" class="btn btn-inverse">Cancel</button>
    </div>
  </div>
  
  </form>

My view

@login_required
def DrinkBeerConfirm(request):
    if request.method == 'POST':
        if request.POST['date']:
            cellar_id = request.session['cellar'] #Get cellar record ID
            cellarEntry = Cellar.objects.get(pk=cellar_id)
            journal = Journal() #make journal entry
            journal.user = request.user
            journal.beer = cellarEntry.beer
            journal.date = request.POST['date']
            journal.notes = request.POST['notes']
            journal.rating = request.POST['rating']
            journal.image = request.FILES['image']
            journal.servingType = request.POST['servingType'] 
            beer = beer.objects.get(id = cellarEntry.beer) #update beer ratings
            currentRating = beer.rating * beer.numRatings 
            beer.numRatings = beer.numRatings + 1
            beer.rating = currentRating / beer.numRatings
            """ if cellarEntry.container == 'KG': #update stock quantity for keg
                cellarEntry.vol = cellarEntry.vol - serving_qty
                if cellarEntry.vol <= 0:
                    cellarEntry.delete()
                else:
                    cellarEntry.save()
                    #display a toast here and delete entry 
            else:
                cellarEntry.qty = cellarEntry.qty - 1 """
            if cellarEntry.qty <= 0:
                cellarEntry.delete()
            else:
                    cellarEntry.save()
            journal.save() #Save journal, beer and cellar records
            beer.save()

As far as I can tell, this is all correct: I have enctype="multipart/form-data" set for the form, and the form fields for the file and submit look OK. I feel like I'm missing something obvious...

1
where is your form? you have to build a form for Journal model first. take a look at this one: stackoverflow.com/questions/65383251/…Reza GH
and you need to add request.FILES too like this: form = JournalForm(request.POST, request.FILES)Reza GH
The form is in the first code snippet.Blacknoise
@Blacknoise Why do you have 4 opening div blocks and 5 div closing blocks?unknown
I've fixed that, but still the same result I'm afraid.Blacknoise

1 Answers

0
votes

I guess you incorrect close tag , it affects to POST request. Check that.