在Django中尝试我的第一个应用程序,并在表单验证方面遇到一些问题(以下文档)
这是文件上传代码的一部分。表格很好。但是,验证失败了。
即使我提供Title
,它也会This field is required.
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
#TODO handle(request.FILES['file'])
return HttpResponseRedirect('/')
else:
form = UploadFileForm()
return render_to_response('setup.html', {'form': form},context_instance=RequestContext(request))
from django import forms
class UploadFileForm(forms.Form):
title = forms.CharField()
file = forms.FileField()
<form action="/setup/" method="post">{% csrf_token %}
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
<input type="submit" value="Submit" />
</form>
答案 0 :(得分:0)
title = forms.CharField(required=False)