我正在 Build 一个与django 1.9的联系表格 . 我的表单有默认输入(全名,电子邮件,电话,消息) .

为了使编码器form.py更容易:

class ContactForm(forms.Form):

full_name = forms.CharField(widget=forms.TextInput(attrs={'placeholder': _('Full Name')}),
                            error_messages={'required': _('Full Name is missing')}, label=_('Full Name'))
phone = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': _('Phone')}), label=_('Phone'))
email = forms.EmailField(widget=forms.TextInput(attrs={'placeholder': _('E-mail')}),
                         error_messages={'required': _('E-mail is missing!'), 'invalid': _('Insert a valid e-mail address!')}, label=_('E-mail'))
message = forms.CharField(widget=forms.Textarea(attrs={'rows': 9}),
                          error_messages={'required': _('Message is missing!')}, label=_('Messages'))

为了对抗机器人和垃圾邮件,我已经将reCAPTCHA添加到了成功运行的模板中 .

在我看来,我首先检查POST请求,然后定义联系表单和模型(需要模型将联系人消息存储到DB) . 接下来,我通过grecaptcha_verify验证recaptcha(只是忽略它) .

之后,验证成功检查将完成

if request.POST:
    contact_form = ContactForm(request.POST or None)
    contact = Contact()
    r = grecaptcha_verify(request)
    if contact_form.is_valid() and r["success"]:
        context = {
            'full_name' : contact_form.cleaned_data['full_name'],
            'email' : contact_form.cleaned_data['email'],
            'phone' : contact_form.cleaned_data['phone'],
            'message' : contact_form.cleaned_data['message'],
        }

在我的模板中,验证码div添加在表单中 .

<div class="large-12 columns">
<div class="g-recaptcha" data-sitekey="6LeNhyUTAAAAAN7KdVt8FLoxlVwAYY2zPVUEy7wu"></div>
</div>

目前,everthing工作正常 . 但我想检查发送请求的访问者是否至少在POST请求中发送了g-recaptcha-response . 我能做到这一点 . 但问题从这里开始 .

forms.py 中,如果需要验证参数,则需要使用其名称和参数定义变量( i.E. full_name = forms.CharField(widget = forms.TextInput(attrs = {'placeholder':_('Full Name')})..但是,正如已经告诉过的那样,python将连字符视为operators . 所以我想到的下一个选项是:为什么我只需更改reCAPTCHA类名 . 当然,将其默认名称'g-recaptcha'更改为任何其他类,当我查阅reCAPTCHA文档时,我没有发现任何更改类名的可能性 . 所以我被困在两个特定的要求之间 .

现在,我找到了一个 temporary 解决方案 .

在forms.py中,我定义了一个名为'recaptcha'的新变量 - 它是有效的 .

recaptcha = forms.CharField(widget=forms.TextInput(attrs={'type': _('hidden')}), required=False)

** required设置为false

在模板中,在找到recaptcha的位置,我添加了recaptcha标记的错误警报:

{{ form.recaptcha }}
                {% if form.errors.recaptcha %}
                <div data-alert class="alert callout" data-closable="slide-out-right">
                    {{form.errors.recaptcha.as_text}}
                    <button class="close-button" aria-label="Dismiss alert" type="button" data-close>
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                {% endif %}

在views.py中,我添加了两个if语句:

if not request.POST.get('g-recaptcha-response'):
    contact_form.add_error('recaptcha','No g-captcha-response')
elif not r["success"]:
    contact_form.add_error('recaptcha','Captcha verification not successfull')

这是有效的,但是通过基于g-recaptcha-response参数的request.POST注入错误来欺骗django验证 .

是否有可能更改 g-recaptcha 的类,所以django表单可以加载它而不用连字符问题?