Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Django Simple Captcha with the existing django.contrib.auth.forms

I would like to add captcha on my django registration form using Django Simple Captcha found here: http://code.google.com/p/django-simple-captcha/

This works great if you create a new form but I'm using the django.contrib.auth.forms the one that comes with django. Any idea how I might be able to implement captcha with the existing django auth views? Thank you!

like image 475
avatar Avatar asked Jan 31 '11 20:01

avatar


1 Answers

You could simply subclass the django.contrib.auth.forms forms and add a CaptchaField, like this:

from django.contrib.auth.forms import UserCreationForm
from captcha.fields import CaptchaField

class CaptchaUserCreationForm(UserCreationForm):
    captcha = CaptchaField()

and use the new Form in your view as usual:

if request.POST:
    form = CaptchaUserCreationForm(request.POST)
    if form.is_valid():
        return HttpResponseRedirect('/?ok')
else:
    form = CaptchaUserCreationForm()
like image 126
Marco Avatar answered Nov 15 '22 06:11

Marco