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

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

问题描述

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

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

我正在尝试在我的django重置密码表单中添加无验证码

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

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

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

I have followed the instructions and added the necessay settings:

Installed django-recaptcha to the Python path.

Added captcha to the INSTALLED_APPS setting.

在我的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& google,但找不到合适的东西.

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

推荐答案

您要自定义 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天全站免登陆