django应用程序中表单中的验证码和空字段不正确

时间:2015-03-09 13:17:58

标签: python django forms field captcha

在我的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}))

0 个答案:

没有答案