【问题标题】:Django reverse for 'password_change_done' not found未找到“password_change_done”的 Django 反向
【发布时间】:2021-10-24 12:49:51
【问题描述】:

我正在编写一个小型应用程序,但在 Django 身份验证框架中使用的密码更改元素遇到了一些问题。但是在更改密码后我总是收到错误消息:找不到'password_change_done'的反向。 'password_change_done' 不是有效的视图函数或模式名称。下面是我的代码:

#account urls.py

from django.urls import path, include
from django.contrib.auth import views as auth_views
from . import views

app_name = 'account'

urlpatterns = [
    #Password Changes URLs
    path('password_change/', auth_views.PasswordChangeView.as_view(), name='password_change'),
    path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),

]

这是我登录系统的目录结构: Directory Structure

这里是 password_change.html 文件:

<h3 style="text-align: center;">Change your password</h3>
<div class="login-form" style="text-align: center; ">
   <form method="post">
      {{ form.as_p }}
      <p><input type="submit" value="Change"></p>
      {% csrf_token %}
   </form>
 </div>

任何帮助将不胜感激!

【问题讨论】:

    标签: html python-3.x django


    【解决方案1】:

    由于您使用app_name,因此您需要在PasswordChangeViewsuccess_url 中包含命名空间:

    # account urls.py
    
    from django.urls import path, include
    from django.contrib.auth import views as auth_views
    from . import views
    from django.urls import reverse_lazy
    
    app_name = 'account'
    
    urlpatterns = [
        #Password Changes URLs
        path('password_change/', auth_views.PasswordChangeView.as_view(
            success_url=reverse_lazy('account:password_change_done')
        ), name='password_change'),
        path('password_change/done/', auth_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    ]

    auth 包的其他一些基于类的视图具有success_urls,因此也应该更新这些视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 2015-10-18
      • 2014-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-01-28
      • 2017-05-04
      • 2020-09-09
      相关资源
      最近更新 更多