django 自定义重置密码表单 [英] django customize reset password form

查看:47
本文介绍了django 自定义重置密码表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 django 的初学者(django 1.7 python 2.7).

I am a beginner in django (django 1.7 python 2.7).

我正在尝试将 no captcha recaptcha 添加到我的 django 重置密码表单中.

I am trying to add no captcha recaptcha onto my django reset password form.

我正在尝试使用这个 recaptcha djano 插件.

I am trying to use this recaptcha djano plugin.

我已按照说明进行操作并添加了必要的设置:

I have followed the instructions and added the necessay settings:

将 django-recaptcha 安装到 Python 路径.

在 INSTALLED_APPS 设置中添加验证码.

将以下内容添加到我的 settings.py 文件中:

Added the following to my settings.py file:

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

然后说明建议将验证码添加到表单中,如下所示:

The instructions then advise to add the captcha to the form, like so:

from django import forms
from captcha.fields import ReCaptchaField

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

如何访问内置的重置密码表单?作为初学者,我怀疑我必须自定义内置的重置密码表单,但我该怎么做?我什至不确定内置的重置密码表单在哪里.如何自定义内置的重置密码表单或推送教程的示例会很方便.

How do I access the built in reset password form? As a beginner, I suspect that I have to customise the built in reset password form, but how do I do that? I am not even sure where the built in reset password form is. An example of how to customise the build in reset password form or a push to a tutorial would be handy.

我已经搜索过 SO &谷歌,但找不到任何合适的.

I have searched SO & google, but could not find anything suitable.

推荐答案

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

You want to customise the PasswordReset view. By default, it uses the PasswordResetForm, which you can customize.

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

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

然后在您的 urls.py 中,导入您的表单,并使用 form_class 指定表单.

Then in your urls.py, import your form, and use the form_class to specify the form.

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,需要自定义password_reset视图的URL格式,并将password_reset_form设置为

For Django < 1.11, you need to customise the URL pattern for the password_reset view, and set password_reset_form to

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 中包含密码重置视图的更多信息,请参阅 文档.

For more information about including password reset views in your urls, see the docs.

这篇关于django 自定义重置密码表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆