[Answered ]-Django reCAPTCHA does not validate the response

2๐Ÿ‘

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.

๐Ÿ‘คJingo

0๐Ÿ‘

There is django-recaptcha it would be simply and clearly

Leave a comment