我正在尝试将 django-recaptcha 与 django-registration 集成。我已经确保 django-registration 有效。然后我根据它的文档(django-recaptcha 0.0.6)安装和配置 django-recaptcha。
我captcha = ReCaptchaField()
在RegistrationForm
类中添加registration/forms.py
如下:
from captcha.fields import ReCaptchaField
class RegistrationForm(forms.Form):
required_css_class = 'required'
username = forms.RegexField(regex=r'^[\w.@+-]+$', max_length=30, label=_("Username"), error_messages={'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")})
email = forms.EmailField(label=_("E-mail"))
password1 = forms.CharField(widget=forms.PasswordInput, label=_("Password"))
password2 = forms.CharField(widget=forms.PasswordInput, label=_("Password (again)"))
captcha = ReCaptchaField()
...
验证码确实出现了,但无论我在 chptcha 文本中输入什么(不输入、正确或不正确),在按下“底部”后,它总是显示“此字段是必填项”。(当然我已经输入了两个密码字段)。
它不应该是私钥/公钥问题,因为错误设置验证码不会显示,而是错误文本。任何想法?
顺便说一句,我使用 python 2.7 和 django 1.4.3。而且我测试了两个浏览器:chrome 和 IE9。
[更新]
我发现这个问题的根本原因是因为我输入的文本没有传递给 POST 请求,如下所示:
POST:<QueryDict: {u'username': [u'test123'], u'password1': [u'123'], u'csrfmiddlewaretoken': [u'BUvEURhlUMYDx1DjztgdRuK1CrI7WanY'], u'email': [u'test@gmail.com'], u'password2': [u'123']}>,
客户端浏览器上显示的 html 源代码显示如下。通常它应该在 POST 请求中包含“ recaptcha_challenge_field ”,但我不确定为什么客户端浏览器不会在 POST 请求中发送这个变量。我对java脚本完全陌生。任何想法?
<!doctype html>
<html>
<head>
<title>Register for an account</title>
</head>
<body>
<table>
<form method='post' action=''><div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='BUvEURhlUMYDx1DjztgdRuK1CrI7WanY' /></div>
<tr class="required"><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" name="username" maxlength="30" /></td></tr>
<tr class="required"><th><label for="id_email">E-mail:</label></th><td><input type="text" name="email" id="id_email" /></td></tr>
<tr class="required"><th><label for="id_password1">Password:</label></th><td><input type="password" name="password1" id="id_password1" /></td></tr>
<tr class="required"><th><label for="id_password2">Password (again):</label></th><td><input type="password" name="password2" id="id_password2" /></td></tr>
<tr class="required"><th><label for="id_captcha">Captcha:</label></th><td><script type="text/javascript">
var DjangoRecaptchaOptions = {
"lang": "en"
};
if (typeof RecaptchaOptions !== 'object') {
RecaptchaOptions = DjangoRecaptchaOptions;
} else {
for (key in DjangoRecaptchaOptions) {
RecaptchaOptions[key] = DjangoRecaptchaOptions[key];
}
}
</script>
<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl=en"></script>
<noscript>
<iframe src="https://www.google.com/recaptcha/api/noscript?k=6LeuNO4SAAAAAAdkaCUi6ybtISPI-YhIlOadgFNF&hl=en" height="300" width="500" frameborder="0"></iframe><br />
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type='hidden' name='recaptcha_response_field' value='manual_challenge' />
</noscript>
</td></tr>
<tr><td></td><td><input type="submit" value="Send activation email" /></td>
</form>
</table>
</body>
</html>