开发者

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

开发者 https://www.devze.com 2023-02-07 14:53 出处:网络
I would like to add captcha on my django registration form usingDjango Simple Captcha found here: http://code.google.com/p/django-simple-captcha/

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开发者_Go百科 how I might be able to implement captcha with the existing django auth views? Thank you!


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消