0
我有以下表單,它允許用戶提交文件和電子郵件地址。Django:輸入無效數據時阻止表單域被清除
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))
如果用戶提交表單與無效的電子郵件時,顯示的形式返回給用戶,但字段被清除。什麼是防止這種表單字段清除的正確方法?
顯示您的看法。 – 2010-12-03 14:58:41