首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >request.FILES总是空的

request.FILES总是空的
EN

Stack Overflow用户
提问于 2021-02-17 12:12:34
回答 1查看 196关注 0票数 1

当我试图发布包含文件字段的Django表单时,文件字段将作为request.POST而不是request.FILES (为空)的一部分传递。这会在提交表单时抛出一个MultiValueDictKeyError。

表单html

代码语言:javascript
复制
<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>

我的观点

代码语言:javascript
复制
@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()

据我所知,这一切都是正确的:我为表单设置了enctype="multipart/form-data",为文件设置了表单字段,并提交了OK。我觉得我错过了一些明显的东西..。

EN

回答 1

Stack Overflow用户

发布于 2021-02-17 12:31:52

我想你不正确的关闭标签,它会影响张贴请求。看看那个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66241740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档