29

I want to implement user registration using captcha in Django. The workflow of django-registration app is a great, but it doesn't have captcha.

What captcha would you recommend to use with it? Are there some other variants of registration+captcha or useful links on the topic?

This should work with Django-1.1 and don't be too hard to install.

TechZen
  • 64,370
  • 15
  • 118
  • 145
gleb.pitsevich
  • 1,269
  • 2
  • 12
  • 18

3 Answers3

21

I've just had this problem, but the solution is dead simple.

I'm using django-registration, and I want a reCAPTCHA field for user registration. In just 1 minute:

  1. download django-recaptcha (pip install django-recaptcha)

  2. install it on your project. That is, copy the "captcha" folder to your project, add "captcha" to INSTALLED_APPS and add your RECAPTCHA_PUBLIC_KEY and RECAPTCHA_PRIVATE_KEY keys to settings.py too (as described in the installation instructions)

  3. open registration/forms.py and add this field inside class RegistrationForm(forms.Form):

    captcha = ReCaptchaField()

    you will also have to import:

    from captcha.fields import ReCaptchaField

And that's it. Less than a minute.

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
Salvatorelab
  • 11,614
  • 6
  • 53
  • 80
  • 3
    No kidding - followed your instructions and it took less than a minute.. Thanks! – PhilBot Nov 10 '13 at 02:05
  • 2
    But what if the registration package is not copied into your project and instead installed via pip install ... ? – Timbadu Jan 19 '17 at 15:07
21

django-registration is pretty extendable. One way to extend it is to provide a custom registration form. I'd recommend to use reCaptcha, e.g. with the widget and form field from here (archived). Then it is as simple as writing a custom form class and registration backend (which is simpler than it sounds):

from registration.backends.default import DefaultBackend
from registration.forms import RegistrationForm

class RecaptchaRegistrationForm(RegistrationForm)
    recaptcha = ReCaptchaField(label="I'm a human")

class RecaptchaRegistrationBackend(DefaultBackend):
    def get_form_class(self, request):
        return RecaptchaRegistrationForm

The last step is to tell django-registration to use your backend. That step is described in the docs (I couldn't find a HTML version of the docs, sorry)

Benjamin Wohlwend
  • 30,958
  • 11
  • 90
  • 100
  • the link to the widget and form field gives a 502 – sacabuche Jul 29 '11 at 23:32
  • Fortunately, archive.org has a copy: http://web.archive.org/web/20090606022756/http://lobstertech.com/2008/aug/27/integrating_django_recaptcha/ – Benjamin Wohlwend Jul 30 '11 at 18:28
  • 3
    You can avoid subclassing registration backend setting the form class in your `urls.py` as backend argument. For example: `(r'^register/$', RegistrationView.as_view(form_class=RegistrationForm))`. – Anatoly Scherbakov Dec 22 '13 at 14:06
16

For those like me arriving late to the thread, there are a bunch of solutions out there now, which are pretty easy to install:

I've successfully setup Django Mollom and Django Simple Captcha, and the hardest part was yak shaving around installing PIL on my Mac. Implementing the code was as straightforward as the docs for each would suggest.

sillygwailo
  • 2,007
  • 16
  • 13