You need to enable JavaScript to run this app. Django密码重置提交后报错:Reverse for password_reset_complete not found
优惠活动
大模型
产品
解决方案
定价
更多

Django密码重置提交后报错:Reverse for password_reset_complete not found

Django 密码重置提交后报错「Reverse for 'password_reset_complete' not found」

提问详情

使用PasswordResetView实现密码重置功能时,输入新密码点击提交后,Django抛出报错:Reverse for 'password_reset_complete' not found,报错页面截图已提供。
当前路由配置文件(ulrs.py,疑为urls.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')
]

当前使用自定义视图发送密码重置邮件,需排查报错原因及解决方案。


报错原因

核心问题是命名空间与Django内置类视图的默认反向解析逻辑不匹配。
你给该路由分组设置了app_name = 'Authentication',所有路由名默认都需要带上Authentication:前缀才能被正常反向解析,但Django内置的PasswordResetConfirmView提交成功后,默认会直接反向解析全局路由名password_reset_complete,不会自动加上你配置的命名空间前缀,因此无法匹配到你定义的对应路由,抛出反向解析失败的错误。


解决方案

  • 先导入反向解析依赖:在路由文件顶部添加from django.urls import reverse_lazy
  • 给PasswordResetConfirmView手动指定带命名空间的成功跳转地址,修改对应的路由配置:
    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'),
    
  • 检查你自定义的password_reset视图逻辑,若视图内完成发送邮件后是重定向到password_reset_done路由,也要将反向解析地址改为带命名空间的版本:reverse('Authentication:password_reset_done'),否则提交重置申请阶段会提前报同样的反向解析错误。
  • 确认路由文件本身命名为urls.py(而非当前拼写的ulrs.py),且已在项目根路由中正确挂载该子路由,避免出现路由无法被全局识别的低级问题。

内容的提问来源于stack exchange,提问作者user15272133

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.24 16:36:03