我有以下表格,允许用户提交文件和电子邮件地址。
from django import forms
from django.utils.translation import gettext_lazy as _
from django.core.validators import validate_email
class UploadFileForm(forms.Form):
email = forms.EmailField()
file = forms.FileField()
class EmailField(forms.CharField):
default_error_messages = {
'invalid': _(u'Enter a valid e-mail address.'),
}
default_validators = [validate_email]
以下是观点:
def upload_file(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
email = form.cleaned_data['email']
handle_uploaded_file(email, request.FILES['file'])
return HttpResponseRedirect('/thanks')
else:
form = UploadFileForm()
return render_to_response('upload_file.html', {'form': form},
context_instance=RequestContext(request))
如果用户使用无效的电子邮件提交表单,表单将显示回用户,但字段将被清除。什么是阻止表单字段清除的正确方法?
答案 0 :(得分:2)
愚蠢的错误。更新了我的模板以包含表单值。一切正常。