By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

Add captcha to django web page using Python-reCaptcha

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')
    Posted On
  • 02 January 2012
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Querying with Django Q objects

Querying with Django Q objects: Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects ...

Continue Reading...
How to filter a Django Queryset using Extra

Using SQL Queries in Django ORM - Filtering extra objects within in a single query, to reduce the number of queries.

Continue Reading...
Django-REST Framework Object Level Permissions and User Level Permissions

Django-REST User Level Permissions and Object Level Permissions. User Level Permissions and Object level Permissions allow to serve customers based on their access levels or ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties