0

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

视图.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))

表格.py

from django import forms

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

设置.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>
4

1 回答 1

0

将参数添加required到您的表单字段。如果您不提供它,Django 将假定它是必需的

title = forms.CharField(required=False)

于 2012-09-12T03:13:25.513 回答