Django表单验证错误

时间:2012-09-12 03:00:35

标签: python django

在Django中尝试我的第一个应用程序,并在表单验证方面遇到一些问题(以下文档) 这是文件上传代码的一部分。表格很好。但是,验证失败了。 即使我提供Title,它也会This field is required.

views.py

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))

forms.py

from django import forms

class UploadFileForm(forms.Form):
    title = forms.CharField()
    file = forms.FileField()

setup.html

<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>

1 个答案:

答案 0 :(得分:0)

required参数到您的表单字段。 If you don't provide it Django will assume it is required

title = forms.CharField(required=False)