在我的django应用程序中 - 我对表单有一个看法。问题是,当我有错误的验证码时,其他数据(名称,电子邮件,消息)都是空的,应该填入输入的数据。
view.py
def contact(request):
form = ContactForm(request.POST)
key = CaptchaStore.generate_key()
image_url = captcha_image_url(key)
if form.is_valid():
data = request.POST.copy()
data.__delitem__('val')
data.__delitem__('check1')
data.__delitem__('check2')
data.__delitem__('csrfmiddlewaretoken')
msg = '\n'.join(['%s %s' % (i, data[i]) for i in data if data[i]])
try:
mail_managers(_('Contact Form'), msg)
contact_confirm(form.cleaned_data['email'])
except Exception:
return HttpResponse(json.dumps({'success': False, 'key': key, 'image_url': image_url,
'message': _('Sorry there was an error while sending email.')}))
return HttpResponse(json.dumps({'success': True, 'message': _('Message was sent')}))
message = ", ".join(["%s: %s"%x for x in form.errors.iteritems()]).replace("__all__:","")
return HttpResponse(json.dumps({'success': False, 'message': message,
'key': key, 'image_url': image_url}))