将密码重置添加到Django Admin时输入错误的URL? [英] Adding the password reset to Django Admin goes to a wrong URL?

查看:61
本文介绍了将密码重置添加到Django Admin时输入错误的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注

I'm following the section on Adding a password reset feature to the Django Admin, which asks you to add these four paths:

path('admin/password_reset/', auth_views.PasswordResetView.as_view(), name='admin_password_reset'),
path('admin/password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),

添加它们时,忘记了密码或用户名?"链接出现在Django Admin登录屏幕上,如果单击它,它可以正常工作,甚至可以发送电子邮件,但是在收到电子邮件后,我最终遇到此错误.

When I add them, the "Forgotten your password or username?" link appears on the Django Admin log in screen, and if I click it, it works, it even sends the email, but after the email, I end up with this error.

Page not found (404)
Request Method: GET
Request URL:    http://localhost:8000/accounts/password_reset/done/
Raised by:  django.contrib.auth.views.PasswordResetDoneView

404 ...好吧...是的...那不是 password_reset/done 所在的地方.电子邮件中的链接用于: http://localhost:8000/accounts/reset/Mjk/5 ... 9 ,因此,相反,这不是 reset/<uidb64>/<令牌>/.为什么这些URL放在错误的位置,而不是/admin/./accounts/.

404... well... yes... that's not where password_reset/done is. And the link in the email is for: http://localhost:8000/accounts/reset/Mjk/5...9, so, against, that's not where reset/<uidb64>/<token>/. Why are these URLs in the wrong place, /accounts/ instead of /admin/.

我的完整网址格式如下:

My full URL patterns look like this:

urlpatterns = [
    path('admin/password_reset/', auth_views.PasswordResetView.as_view(), name='admin_password_reset'),
    path('admin/password_reset/done/', auth_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    path('reset/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
    path('reset/done/', auth_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    path('admin/', admin.site.urls),
    path('accounts/', include('django.contrib.auth.urls')),
    path("", views.index, name="homepage")
]

我猜是因为这些 path('accounts/',include('django.contrib.auth.urls')),对吗?还有另一种控制它们的方法吗?我主要是想了解发生了什么.

I'm guessing then that those redirects go to /accounts/ because of this path('accounts/', include('django.contrib.auth.urls')), is that correct? is there another way of controlling them? I'm mostly trying to understand what's going on.

推荐答案

要发送包含另一个URL的密码重置电子邮件,我相信您需要添加一个单独的电子邮件模板.为此,您需要创建一个链接到该模板的不同视图.您可以通过子类化Django的auth视图来使用它们,并仅定义需要不同的设置.

To send a password reset email containing another URL, I believe you need to add a separate email template. And to do that, you'll need to create a different view that links to that template. You can use Django's auth views by subclassing them and only define the settings you need to be different.

以下是一般创建自定义密码重置视图的方法,该视图可与默认视图一起使用:

Here's how to create a custom password reset view generally, which can be used alongside the default one:

# urls.py

from .views import CustomPasswordResetView

urlpatterns = [
    ...
    # URL to submit your email address for reset link
    path('admin/password-reset', CustomPasswordResetView.as_view(), name='custom_password_reset'),
    # URL link in email, lands on password reset form
    path('admin/reset/<uidb64>/<token>/', CustomPasswordResetConfirmView.as_view(), name='custom_password_reset_confirm'),
    ...
]

# views.py

from django.contrib.auth.views import PasswordResetView

class CustomPasswordResetView(PasswordResetView):
    email_template_name = 'registration/custom_password_reset_email.html'

默认情况下,Django在 django/contrib/admin/templates/registration/password_reset_email.html 中使用密码重置电子邮件模板.也有与密码重置有关的默认HTML模板.

By default, Django uses the password reset email template in django/contrib/admin/templates/registration/password_reset_email.html. The default HTML templates relating to password resets are there too.

要指定您自己的模板,请在项目模板中的某个位置创建另一个 registration 目录(例如, users/templates/registration/,然后在此处创建文件.请确保提供该名称与Django默认模板的名称不同,例如 registration/custom_password_reset_email.html ,然后将默认电子邮件文本复制并粘贴到其中,然后将命名URL更改为所需的URL,即 {%'custom_password_reset_confirm'%} .

To specify your own template, create another registration directory somewhere in your project templates (e.g. users/templates/registration/ and create the file there. Make sure to give it a different name from Django's default templates, e.g. registration/custom_password_reset_email.html, then copy and paste the default email text into it, and change the named URL to the one you want, i.e. {% 'custom_password_reset_confirm' %}.

您可以子类化其他相关视图( PasswordChange PasswordChangeDone 等),以根据需要覆盖其他默认设置,例如HTML模板,表单类和重定向URL.所有密码重置视图都位于 django中.contrib.auth.views .auth视图以基于类的视图的形式提供(与较早的基于标准函数的视图(从Django 2.1版开始删除)一起).

You can subclass the other relevant views (PasswordChange, PasswordChangeDone etc.) to override other default settings as needed, such as HTML template, form class and redirect URL. All the password reset views are in django.contrib.auth.views. The auth views are provided as class-based views (alongside the older standard function-based views, which were removed as of Django version 2.1).

这篇关于将密码重置添加到Django Admin时输入错误的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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