【问题标题】:request.FILES.getlist('file') is emptyrequest.FILES.getlist('file') 为空
【发布时间】:2014-07-29 12:54:44
【问题描述】:

我使用dropzone.js 向请求发送了几个文件,但request.FILES.getlist() 似乎完全是空的。有什么可能的原因吗?

--对不起,这只是我的问题中的一个错字。在我的代码中是FILES

def upload(request):
    user = request.user
    theAccount = user.us_er.account
    if request.method == "POST":
        form = uploadForm(request.POST)
        if form.is_valid():
            descriptions = request.POST.getlist('descriptions')
            count = 0 
            for f in request.FILES.getlist('file'): 
                theAccount.file_set.create(docFile = f, description = descriptions[count], dateAdded = timezone.now(), creator = user.username)
                 count = count + 1
            return HttpResponseRedirect('/home/')

        else:
            return HttpResponse("form is not valid")
    else:
        return HttpResponse('wasnt a post')

这是我的包含 dropzone 的模板。

<form method="POST" style="border: 2px solid green;" action= "/upload/" enctype="multipart/form-data" class="dropzone">
        {% csrf_token %}
<div class="dropzone-previews"></div>


 <button  value=" submit" class="btn btn-success" type="submit" id="submit">Press to upload!</button>
        </form>

【问题讨论】:

  • 您需要发布一些代码 sn-ps 并向我们展示您尝试过的内容等。这没什么好继续的。这也可能对您有用:stackoverflow.com/questions/851336/…
  • 它是request.FILES,而不是request.FILE,并且您的表单应该具有method="post"enctype="multipart/form-data" 属性
  • 谢谢卡尔。我以前看过这篇文章,并且已经按照他们在这篇文章中的方式实现了多个文件。
  • 你的表单需要通过两个字典,像这样form = uploadForm(request.POST, request.FILES)
  • 同样的问题。有人找到解决办法吗?

标签: python django


【解决方案1】:

我知道这是一个老问题,但为了人们通过谷歌登陆这里。

Dropzone.js 使用 ajax 将文件上传到队列中,因此您的端点应该将上传处理为单个文件而不是多个文件。

您可以通过request.FILES['file']访问该文件

【讨论】:

    【解决方案2】:

    而不是这样做: descriptions = request.POST.getlist ('descriptions')

    试试这样: descriptions = request.FILES.getlist ('descriptions []')

    在我的情况下它有效。

    【讨论】:

    • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2016-07-15
    • 2015-12-24
    • 1970-01-01
    • 2018-04-15
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多