我在测试django项目中使用request.FILES,但是字典始终为空
我使用django 1.11.17
#views.py
class UploadMenuView(CreateView):
def post(self, request, *args, **kwargs):
file = request.FILES.popitem()
print(file.name)
return HttpResponse(file.name)
#templates/admin/change_list.html
...
<form method="post" action="{% url 'dashboard:upload-menu' %}">
{% csrf_token %}
<input type="file" value="Select file" name="file"/>
<input type="submit" value="Upload menu"/>
</form>
...
#urls.py
url(r'upload-menu/', UploadMenuView.as_view(), name='upload-menu')
我所有的尝试都以异常值结尾:“ popitem():字典为空”。但是我在F12中看到我的文件作为POST请求。键:文件,值:file.txt
答案 0 :(得分:0)
添加到template / admin / change_list.html中的表单
enctype="multipart/form-data"
之后,从文件中获取文本:
file = request.FILES['file'].read()