python - 此字段是必需的错误,但字段在表单中
问题描述
所以我有一个用 request.POST 初始化的表单,但是,即使表单有正确的字段,它也是无效的,我错过了什么?
def some_view(self, request):
form = SomeForm(request.POST or None)
if form.is_valid():
(Pdb) form.data
<QueryDict: {u'csv_file': [u'some_csv_file.csv'], u'csrfmiddlewaretoken': [u'csrftoken']}>
(Pdb) form.is_valid()
False
(Pdb) form.errors
{'csv_file': [u'This field is required.']}
解决方案
当您在 Django 表单中有一个 FileField 时,有两件事要做:
1. In your template, in the form tag, add enctype="multipart/form-data"
<form method="POST" action="" enctype="multipart/form-data">
2. Pass the request.FILES when creating the form
form = SomeForm(request.POST or None, request.FILES)
推荐阅读
- linux - 在linux中用另一个字符串替换两列中某些行开头的字符串
- node.js - 什么是使用 Javascript 运行 Discord 机器人的好程序?
- javascript - 如何在一个按钮中实现两个 onclicks?
- java - 没有实体时如何在 Spring 中自定义序列化
- docker - 如何以编程方式在 Docker 中设置 Kubernetes?
- r - 从循环中导出系数以进行多重 cox 回归
- python - 尝试更新命令行输入
- c# - 使用 Asp.net mvc 上传图片
- svelte - Svelte 中的 X-callback-url
- html - 隐藏具有“0”值的文本