我按照django-recaptcha的说明在我的联系表单中添加了一个字段。当它呈现时(在我的本地测试中),它有一个显示“Captcha:”的字段,但没有呈现实际的验证码。chrome中的开发控制台说
Uncaught Error: Missing required parameters: sitekey
在https://www.gstatic.com/recaptcha/api2/v1531759913576/recaptcha__en.js
这个答案说要添加render=explicit
到javascript中,但是a)我不确定这是否是问题,b)我认为Django包会处理它。
Django 1.11.8,Django 重新验证 1.4.0。
Django形式是
class ContactForm(forms.Form):
contacter = forms.EmailField(required=True,
label=_('Your Email (optional)'),
widget=forms.widgets.TextInput(
attrs={'size': '50'}))
contact_text = forms.CharField(required=True,
widget=forms.widgets.Textarea(
attrs={'rows': '10',
'cols': '70',
'class': 'defaultText',
'title':
_('Type a message here')}))
captcha = ReCaptchaField()
渲染的 Django 表单如下。
<form action="/contact/"
method="post" id="new-contact">
<input type='hidden' name='csrfmiddlewaretoken' value='...' />
<fieldset id="contact">
<legend>Contact Us</legend>
<tr><th><label for="id_contacter">Your Email (optional):</label></th><td><input type="text" name="contacter" required id="id_contacter" size="50" /></td></tr>
<tr><th><label for="id_contact_text">Contact text:</label></th><td><textarea name="contact_text" rows="10" title="Type a message here" id="id_contact_text" required cols="70" class="defaultText">
</textarea></td></tr>
<tr><th><label for="id_captcha">Captcha:</label></th><td><script src="https://www.google.com/recaptcha/api.js?hl=en"></script>
<div class="g-recaptcha" data-sitekey="" data-required="True" data-id="id_captcha" ></div>
<noscript>
<div style="width: 302px; height: 352px;">
<div style="width: 302px; height: 352px; position: relative;">
<div style="width: 302px; height: 352px; position: absolute;">
<iframe src="https://www.google.com/recaptcha/api/fallback?k="
frameborder="0" scrolling="no"
style="width: 302px; height:352px; border-style: none;">
</iframe>
</div>
<div style="width: 250px; height: 80px; position: absolute; border-style: none;
bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">
<textarea id="g-recaptcha-response" name="g-recaptcha-response"
class="recaptcha_challenge_field"
style="width: 250px; height: 80px; border: 1px solid #c1c1c1;
margin: 0px; padding: 0px; resize: none;" value="">
</textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge" />
</div>
</div>
</div>
</noscript></td></tr>
</fieldset>
<input type="submit" value="Submit" />
</form>