我正在尝试在我的表单上实现Django reCAPTCHA。问题是,它没有每次验证reCAPTCHA会话,我正确提交表单。我尝试提交reCAPTCHA表单大约30次,但仍然无法验证reCAPTCHA。
我在开发服务器上实现了reCAPTCHA,并将域名添加为www.example.com。
我也在使用本教程http://www.chrisumbel.com/article/recaptcha_with_django
这可能是原因,它不会验证因为,我正在使用开发,否则有人可以帮助我吗?
from django.contrib.auth.views import password_reset
from django.shortcuts import render
from recaptcha.client import captcha
def forgot_password(request):
if request.method == 'POST':
response = captcha.submit(request.POST.get('recaptcha_challenge_field'), request.POST.get('recaptcha_response_field'), '1231d12dsad12', request.META['REMOTE_ADDR'],)
if response.is_valid:
captcha_response = "YOU ARE HUMAN: %(data)s" % {'data' : edit_form.data['data_field']}
else:
captcha_response = 'YOU ARE ROBOT'
return render(request, 'forgot_password.html',{'captcha_response':captcha_response})
else:
return render(request, 'forgot_password.html')
模板
{% block title %}Forgot Password<br>{% endblock title %}
<form method="post" action="{% url accounts:forgot-password %}">
{% csrf_token %}
<p>Please enter your email address.
You will receive a link to create a new password via email.</p>
<input type="email" name="email"
placeholder="Your e-mail"><br/>
<th>Are you human?</th>
<span class="validation_error">{{ captcha_response }}</span>
<script type="text/javascript"
src="http://api.recaptcha.net/challenge?k=123dwqsdasd123e23d32d32">
</script>
<noscript>
<iframe src="http://api.recaptcha.net/noscript?k=123dwqsdasd123e23d32d32">
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>
<button type="submit">Send new password</button>
</form>
答案 0 :(得分:2)
您可以自己实施:
import urllib, urllib2, re
def recaptcha(request, postdata):
rc_challenge = postdata.get('recaptcha_challenge_field','')
rc_user_input = postdata.get('recaptcha_response_field', '').encode('utf-8')
url = 'http://www.google.com/recaptcha/api/verify'
values = {'privatekey' : 'PRIVATE-KEY', 'remoteip': request.META['REMOTE_ADDR'], 'challenge' : rc_challenge, 'response' : rc_user_input,}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
answer = response.read().split()[0]
response.close()
return answer
当输入验证码时返回true,否则返回false。
在你看来,你可以这样做:
if request.method == "POST":
postdata = request.POST.copy()
captcha = recaptcha(request, postdata)
if captcha:
#do something
else:
#do something else
您必须稍微调整模板,不应该难以弄清楚。希望这会导致正确的方向。
答案 1 :(得分:0)
django-recaptcha它简单明了