【问题标题】:Reverse for 'password_reset_complete' not found未找到“password_reset_complete”的反向
【发布时间】:2021-04-22 08:06:38
【问题描述】:

我正在使用PasswordResetView 重置密码,但是当我输入新密码并提交按钮时。 Django 重定向到这个

ulrs.py

from django.urls import path
from Authentication import views
from django.contrib.auth import views as auth_view
from .forms import *

app_name = 'Authentication'

urlpatterns = [
    path('', views.loginpage, name='loginpage'),
    path('login', views.handlelogin, name='login'),
    path('logout', views.handlelogout, name='logout'),
    path('password_reset', views.password_reset, name='password_reset'),

    path('password_reset/done/',
         auth_view.PasswordResetDoneView.as_view(template_name='Authentication_template/password_reset_done.html'),
         name='password_reset_done'),

    path('reset/<uidb64>/<token>/',
         auth_view.PasswordResetConfirmView.as_view(template_name='Authentication_template/password_reset.html',
                                                    form_class=SetPasswordForm), name='password_reset_confirm'),

    path('password_reset_complete/', auth_view.PasswordResetCompleteView.as_view(
        template_name='Authentication_template/password_reset_complete.html'),
         name='password_reset_complete')
]

这里我使用自定义视图向用户发送电子邮件。

【问题讨论】:

  • 你设置了app_name = 'Authentication' 意味着你有命名空间这些url。所以你现在需要将它们称为Authentication:password_reset_complete 而不是password_reset_complete 等。参见URL namespaces (Django docs)
  • @AbdulAzizBarkat 我知道命名空间的东西,但这里的问题是不同的,当我提交密码重置表单时,它必须重定向到password_reset_complete/,但它给了我屏幕截图中的错误
  • 问题是命名空间。您收到错误是因为视图本身尝试将 url 反转为 reverse_lazy('password_reset_complete') 而相反,由于您的命名空间,它需要是 reverse_lazy('Authentication:password_reset_complete')...

标签: python django django-views django-authentication


【解决方案1】:

你设置了app_name = 'Authentication' 意味着你有命名空间这些网址。所以你现在需要将它们称为Authentication:password_reset_complete 而不是password_reset_complete 等。参见URL namespaces (Django docs)

一种解决方案是删除app_name = 'Authentication' 行,如果您希望保留这些行,您需要自己为视图设置success_url,您可以将其作为kwarg 传递给as_view

from django.urls import reverse_lazy


path('reset/<uidb64>/<token>/',
    auth_view.PasswordResetConfirmView.as_view(
        template_name='Authentication_template/password_reset.html',
        form_class=SetPasswordForm,
        success_url = reverse_lazy('Authentication:password_reset_complete')
    ), name='password_reset_confirm'
)

【讨论】:

    【解决方案2】:

    您的 password_reset_complete 似乎使用了错误的路径名。

    而不是路径('password_reset_complete/'...

    尝试:

    path('reset/done/', auth_view.PasswordResetCompleteView.as_view(
        template_name='Authentication_template/password_reset_complete.html'),
         name='password_reset_complete')
    

    【讨论】:

      猜你喜欢
      • 2018-01-28
      • 1970-01-01
      • 2020-09-09
      • 2019-04-09
      • 2019-10-19
      • 2018-06-28
      • 2021-10-24
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多