django自定义重置密码表单

问题描述 投票:3回答:1

我是一个Django的初学者(Django 1.7 python 2.7)。

我正在尝试添加 无验证码 重新验证码 到我的django重置密码表单上。

我想用这个 recaptcha djano插件.

我已经按照说明添加了必要的设置。

Installed django-recaptcha to the Python path.

Added captcha to the INSTALLED_APPS setting.

在我的settings.py文件中添加了以下内容。

RECAPTCHA_PUBLIC_KEY = '76wtgdfsjhsydt7r5FFGFhgsdfytd656sad75fgh' # fake - for the purpose of this post.
RECAPTCHA_PRIVATE_KEY = '98dfg6df7g56df6gdfgdfg65JHJH656565GFGFGs' # fake - for the purpose of this post.
NOCAPTCHA = True

说明书建议在表单中添加验证码,就像这样。

from django import forms
from captcha.fields import ReCaptchaField

class FormWithCaptcha(forms.Form):
    captcha = ReCaptchaField()

我如何访问内置的重置密码表单?作为一个初学者,我怀疑我必须定制内置的重置密码表单,但我怎么做呢?我甚至不知道内置的重置密码表在哪里。 如果能给我一个如何自定义内置重置密码表的例子或者推送一个教程就更好了。

我已经搜索了SO&谷歌,但无法找到任何合适的。

python django django-forms recaptcha reset-password
1个回答
4
投票

你想自定义 PasswordReset 视图。默认情况下,它使用 PasswordResetForm,你可以自定义。

# in e.g. myapp/forms.py
from django.contrib.auth.forms import PasswordResetForm

class CaptchaPasswordResetForm(PasswordResetForm):
    captcha = ReCaptchaField()
    ...

然后在您的 urls.py导入你的表格,然后使用 form_class 来指定表单。

from django.contrib.auth import views as auth_views
from django.urls import path
from web.forms import CaptchaPasswordResetForm

urlpatterns = [
    path("accounts/password_reset/", auth_views.PasswordResetView.as_view(form_class=CaptchaPasswordResetForm)),
]

对于Django < 1.11来说,你需要自定义URL模式,为 password_reset 观点,并设定 password_reset_form

from django.contrib.auth import views as auth_views
from myapp.forms import CaptchaPasswordResetForm

urlpatterns = [
    ...
    url(
        r'^password_reset/',
        auth_views.password_reset,
        {'password_reset_form': CaptchaPasswordResetForm},
    )
]

关于在URL中包含密码重置视图的更多信息,请参阅 文献.

© www.soinside.com 2019 - 2024. All rights reserved.