此字段为必填错误,但格式为

时间:2019-02-19 09:00:03

标签: python django django-forms

所以我有一个用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.']}

1 个答案:

答案 0 :(得分:1)

当您以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)