7

reCaptcha for Django’s comments framework

Posted July 26th, 2011 in Python by Florentin

This is how you add reCaptcha (the captcha system built by Google) to the Django’s comments framework.

Requirements

For this task you will need to build a custom module which depends on django-recaptcha-works. recaptcha-works provides a special Form field named “RecaptchaField”
and a decorator function “fix_recaptcha_remote_ip” which inserts the request’s REMOTE_ADDR (IP) into the request.POST data.

django-recaptcha-works can be downloaded from http://pypi.python.org/pypi/django-recaptcha-works/ or install it with:
pip install django-recaptcha-works

You also need a pair of reCaptcha keys from this address:

https://www.google.com/recaptcha/admin/create

Build a custom comment module

Create a new comment module, add it to INSTALLED_APPS and set a special settings COMMENTS_APP. The process is described on this page:

https://docs.djangoproject.com/en/dev/ref/contrib/comments/custom/

cd /path/to/your/project
mkdir my_comment_app

Make these changes to your settings file (usually settings.py):

INSTALLED_APPS = [
...
'my_comment_app',
...
]
COMMENTS_APP = 'my_comment_app'
RECAPTCHA_PUBLIC_KEY = 'public key'
RECAPTCHA_PRIVATE_KEY = 'private key'

recaptcha-works provides more settings, please check the settings.py from inside the recaptcha-works module.
Here are some interesting settings:

RECAPTCHA_USE_SSL = False
RECAPTCHA_OPTIONS = {
        'theme': 'white',
        'lang': 'en',
        'tabindex': 0,
}
RECAPTCHA_VALIDATION_OVERRIDE = False

Inside your new module “my_comment_app”, create these files:

__init__.py

from .forms import CustomCommentForm
def get_form():
return CustomCommentForm

forms.py

from django.contrib.comments.forms import CommentForm
from django.contrib.comments.models import Comment
from recaptcha_works.fields import RecaptchaField

class CustomCommentForm(CommentForm):
    recaptcha = RecaptchaField(label='Human test', required=True)
    def get_comment_model(self):
        return Comment

models.py

from django.db import models
from django.contrib.comments.models import Comment

class CustomComment(Comment):
    pass

urls.py

from django.conf.urls.defaults import patterns, include, url
from . import views

urlpatterns = patterns("",
    url(r'^post/$', views.custom_post_comment, name='comments-post-comment'),
    url(r'', include('django.contrib.comments.urls')),
)

views.py

from django.contrib.comments.views.comments import post_comment
from recaptcha_works.decorators import fix_recaptcha_remote_ip

def custom_post_comment(request, next=None, using=None):
    return fix_recaptcha_remote_ip(post_comment)(request, next, using)

You can download a sample of the module here.

7 Responses so far.

  1. Alejandro says:

    Thanks a lot for the useful information. I used your snippets and it works! However, I think the couldn’t make the reCaptcha work for localhost.

  2. Florentin says:

    Hey Alejandro, could you tell me why the reCaptcha didn’t work on localhost ?

  3. Alejandro says:

    Sorry, I misunderstood and the error is related to the remote IP: “CAPTCHA failed due to no visible IP address.”

  4. Arthur Alvim says:

    Is there any way to overcome these so we could test the ReCaptcha in localhost??

  5. Mike says:

    You can overcome that by having a subdomain on the domain name that you registered with Google. So if you’re site is example.com, you can add a HOSTS entry for testbox.example.com to point to localhost and recaptcha will work fine.

  6. keos says:

    ta bueno esto

Leave a Reply