Reputation: 1943
I'm trying to implement Django reCAPTCHA on my forms . The problem is , it does not validate the reCAPTCHA session everytime , I correctly submit the form. I attempted to submit the reCAPTCHA form about 30 times and it still wouldn't validate the reCAPTCHA.
I've implemented the reCAPTCHA on development server, and I added the domain as www.example.com .
I'm also using this tutorial http://www.chrisumbel.com/article/recaptcha_with_django
Can this be the reason why , it won't validate because , I'm using development otherwise can someone please help me ?
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')
template
{% 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>
Upvotes: 1
Views: 1449
Reputation: 3240
You could do your own implementation:
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
This returns true when captcha was typed right else false.
In your view you could then do something like this:
if request.method == "POST":
postdata = request.POST.copy()
captcha = recaptcha(request, postdata)
if captcha:
#do something
else:
#do something else
You would have to adjust your template a bit, should not be to hard to figure it out. Hope this leads into the right direction.
Upvotes: 2
Reputation: 1938
There is django-recaptcha it would be simply and clearly
Upvotes: 0