ReCaptcha in Django Forms

Check out the new site at https://rkblog.dev.

ReCaptcha is a popular system for bot-blocking forms. The API allows integration of the ReCaptcha widget on our sites. In case of Python we can use recaptcha-client, which is used by a Django sippet adding form field for Django forms. In this article I'll show you simple example based on that snippet

  • You have to start with making a set of keys for your domain
  • Then you can add them to settings.py of your Django project:
    RECAPTCHA_PUBLIC_KEY = '*Key here*'
    RECAPTCHA_PRIVATE_KEY = '*Key here*'
    
  • Install recaptcha-client through easy_install or other way
  • Now we have to add the field and widget definition for Djang forms. It can be done like so - create a folder "recaptchawidget" in your project folder. Add 3 files in it: fields.py, widgets.py and __init__.py.
Code for widgets.py:
from django import forms
from django.utils.safestring import mark_safe
from django.conf import settings
from recaptcha.client import captcha
    
class ReCaptcha(forms.widgets.Widget):
        recaptcha_challenge_name = 'recaptcha_challenge_field'
        recaptcha_response_name = 'recaptcha_response_field'
    
        def render(self, name, value, attrs=None):
            return mark_safe(u'%s' % captcha.displayhtml(settings.RECAPTCHA_PUBLIC_KEY))
    
        def value_from_datadict(self, data, files, name):
            return [data.get(self.recaptcha_challenge_name, None), 
                data.get(self.recaptcha_response_name, None)] 
Code for fields.py:
from django.conf import settings
from django import forms
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext_lazy as _

from recaptchawidget.widgets import ReCaptcha
from recaptcha.client import captcha
    
class ReCaptchaField(forms.CharField):
        default_error_messages = {
            'captcha_invalid': _(u'Invalid captcha')
        }
    
        def __init__(self, *args, **kwargs):
            self.widget = ReCaptcha
            self.required = True
            super(ReCaptchaField, self).__init__(*args, **kwargs)
    
        def clean(self, values):
            super(ReCaptchaField, self).clean(values[1])
            recaptcha_challenge_value = smart_unicode(values[0])
            recaptcha_response_value = smart_unicode(values[1])
            check_captcha = captcha.submit(recaptcha_challenge_value, 
                recaptcha_response_value, settings.RECAPTCHA_PRIVATE_KEY, {})
            if not check_captcha.is_valid:
                raise forms.util.ValidationError(self.error_messages['captcha_invalid'])
            return values[0] 


Now you can use ReCaptcha widgets in your forms. Here is an example form definition:
from django import forms

from recaptchawidget.fields import ReCaptchaField 

class RegisterProForm(forms.Form):
    """
    A form with recaptcha
    """
    login = forms.CharField(min_length=2, max_length=30)
    recaptcha = ReCaptchaField()
And a view that manages the form:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.template import RequestContext

from myform.forms import *

def show_form(request):
    """
    Recaptcha example
    """
    form = RegisterProForm()
    if request.POST:
        form = RegisterProForm(request.POST)
        if form.is_valid():
            print 'ok'
            return HttpResponseRedirect('/')
    
    return render_to_response('myform.html', {'form': form}, context_instance=RequestContext(request))
And the template:
<form action="./" method="post">{% csrf_token %}
    {{ form.login }}{% if form.login.errors %}<div class="errmsg">{{ form.login.errors|join:", " }}</div>{% endif %}<br />
    {{ form.recaptcha }}{% if form.recaptcha.errors %}<div class="errmsg">{{ form.recaptcha.errors|join:", " }}</div>{% endif %}<br />
    
    <input type="submit" value="Send Form" />
    </form>
We are all done. The ReCaptcha widget will be validated as any other Django form field.
recaptcha_django
RkBlog

Django web framework tutorials, 1 February 2011


Check out the new site at https://rkblog.dev.
Comment article
Comment article RkBlog main page Search RSS Contact