使用现有的 django.contrib.auth.forms 实现 Django Simple Captcha

Posted

技术标签:

【中文标题】使用现有的 django.contrib.auth.forms 实现 Django Simple Captcha【英文标题】:Implement Django Simple Captcha with the existing django.contrib.auth.forms 【发布时间】:2011-06-18 20:49:26 【问题描述】:

我想使用 Django Simple Captcha 在我的 django 注册表单上添加验证码:http://code.google.com/p/django-simple-captcha/

如果您创建一个新表单,这会非常有用,但我使用的是 django 附带的 django.contrib.auth.forms。知道如何使用现有的 django auth 视图实现验证码吗?谢谢!

【问题讨论】:

【参考方案1】:

您可以简单地将 django.contrib.auth.forms 表单子类化并添加 CaptchaField,如下所示:

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

class CaptchaUserCreationForm(UserCreationForm):
    captcha = CaptchaField()

并像往常一样在您的视图中使用新表单:

if request.POST:
    form = CaptchaUserCreationForm(request.POST)
    if form.is_valid():
        return HttpResponseRedirect('/?ok')
else:
    form = CaptchaUserCreationForm()

【讨论】:

以上是关于使用现有的 django.contrib.auth.forms 实现 Django Simple Captcha的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Django 的 auth.contrib 模块按用户名查找用户?

渲染时 NoReverseMatch:“django.contrib.auth.views.login”的反向

将 CSS 类添加到 django.contrib.auth.views.login 中的字段

使用 django.contrib.auth.views.password_change 强制执行密码强度要求

南迁移错误:django.contrib.auth 的 NoMigrations 异常

AttributeError:模块'django.contrib.auth.views'没有属性'LoginView'