Add captcha to django web page using Python-reCaptcha

Reading Time : ~ .

Python-reCaptcha is a pythonic and well-documented reCAPTCHA client that supports all the features of the remote API to generate and verify CAPTCHA challenges. To add to your Django project, you need captcha public, private keys are required.

How to integrate reCaptcha with Django?

Following gives you a brief idea of how to integrate reCaptcha to Django application

  • First get the corresponding public key and private key from reCaptcha site and keep them in your settings.py
in settings.py
CAPTCHA_PUBLIC = 'XXXXXXXXXXXXXXXXXX'
CAPTCHA_PRIVATE = 'XXXXXXXXXXXXXXXXXX'
  • Render the reCaptcha to corresponding HTML page
in views.py
recaptcha = captcha.displayhtml(CAPTCHA_PUBLIC) 
    #displayhtml() gives the captcha object to be displayed on html
return render_to_response('demo.html','recaptcha':recaptcha)
in demo.html
<form action="/" method="post">
{{recaptcha|safe}}
<button class="submit" type="submit">Submit</button>
</form>
  • Validate the challenge field and response fields.
in views.py
capresponse = captcha.submit( request.POST.get('recaptcha_challenge_field'),
                              request.POST.get('recaptcha_response_field'),
                              CAPTCHA_PRIVATE,
                              request.META.get('REMOTE_ADDR'))
'''checks if the catpcha is valid or not(challenge field and response fields    are equal or not '''
if capresponse.is_valid:
     do_some_work()
else:
     return HttpResponse('wrong captcha text')
    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django webpacker - A compression tool to bundles css, js files Nikhila Mergu

django-webpacker is a django compressor tool which bundles css, js files to a single css, js file with webpack and updates your html files with ...

Continue Reading...
Integrating Gitlab in django project with gitlab api for gitlab authentication and access Nikhila Mergu

This is a simple way to integrate gitlab authentication in your django apps.

We can get user verified email id, general information, git lab URL ...

Continue Reading...
how to pass extra context data to serializers in django-rest-framework ? Nikhila Mergu

Django rest-framework passes extra context data to serializers for the best design of rest applications. we can use "context" parameter in serializers to pass extra ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties