[Django]-Recaptcha in Django without forms?

1👍

I am just using this python client for recaptcha:

http://pypi.python.org/pypi/recaptcha-client

then my view looks like this:

captcha_key = get_config('RECAPTCHA_PUB_KEY',None)
recaptcha_challenge_field = request.POST.get('recaptcha_challenge_field', None)
recaptcha_response_field = request.POST.get('recaptcha_response_field', None)
check_captcha = captcha.submit(recaptcha_challenge_field, recaptcha_response_field, settings.RECAPTCHA_PRIVATE_KEY, request.META['REMOTE_ADDR'])
if check_captcha.is_valid is False:
    log.info('captcha_error : %s' % check_captcha.error_code)
    return {'TEMPLATE':template_name,'captcha_error': True,'register_form': f,'captcha_key':captcha_key ,'next':redirect_to}

3👍

recaptcha-client doesn’t work with python3. I ended up using django-recaptcha (https://pypi.python.org/pypi/django-recaptcha/1.0). The brief documentation explains how to implement recaptcha using the formfield ‘ReCaptchaField’, but you can just use the submit function from captcha.client like this:

import captcha.client

[…]

recaptcha_response = captcha.client.submit(  
    request.POST.get('recaptcha_challenge_field'),  
    request.POST.get('recaptcha_response_field'),  
    '[[privatekey]]',  
    request.META['REMOTE_ADDR'],)  

Then you check whether recaptcha_response.is_valid.

No need to add recaptcha to your INSTALLED_APPS or anything.

👤wardk

Leave a comment