python - Django 2.0 - 找不到 'password_change_done' 的反向。 'password_change_done' 不是有效的 View 函数或模式名称

标签 python django authentication django-templates

我在尝试更改我的应用密码后收到此错误消息。

你知道是什么导致这条路线失败吗?

实际上,它正在更改密码,但没有呈现成功模板“password_change_done.html”。

谢谢!

app/urls.py

from django.contrib.auth import views as auth_views
from django.urls import path
​
from . import views
​
app_name = 'account'
​
urlpatterns = [
    # path('login/', views.user_login, name='login'),
    path('', views.dashboard, name='dashboard'),
​
    # login / logout urls
    path('login/', auth_views.LoginView.as_view(template_name='registration/login.html'), name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name='logout'),
    path('logout-then-login/', auth_views.logout_then_login, name='logout_then_login'),
​
    # change password 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'),
]
​

​

错误信息

# NoReverseMatch at /account/password-change/
# Reverse for 'password_change_done' not found. 'password_change_done' is not a valid view function or pattern name.
# Request Method:   POST
# Request URL:  http://localhost:8000/account/password-change/
# Django Version:   2.0.4
# Exception Type:   NoReverseMatch
# Exception Value:  
# Reverse for 'password_change_done' not found. 'password_change_done' is not a valid view function or pattern name.
# Exception Location:   C:\env\django_social_website\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 632
# Python Executable:    C:\env\django_social_website\Scripts\python.exe
# Python Version:   3.6.3
# Python Path:  
# ['C:\\Projects\\django_social_website',
#  'C:\\env\\django_social_website\\Scripts\\python36.zip',
#  'C:\\ProgramData\\Anaconda3\\DLLs',
#  'C:\\ProgramData\\Anaconda3\\lib',
#  'C:\\ProgramData\\Anaconda3',
#  'C:\\env\\django_social_website',
#  'C:\\env\\django_social_website\\lib\\site-packages',
#  'C:\\env\\django_social_website\\lib\\site-packages\\setuptools-28.8.0-py3.6.egg',
#  'C:\\env\\django_social_website\\lib\\site-packages\\pip-9.0.1-py3.6.egg']
# Server time:  Thu, 5 Apr 2018 21:34:22 +0000

​

app/templates/registration/password_change_form.html

{% extends "base.html" %}
​
{% block title %}Change your password{% endblock %}
​
{% block content %}
    <h1>Change your password</h1>
    <p>Use the form below to change your password.</p>
    <form action="." method="post">
        {{ form.as_p }}
        {% csrf_token %}
        <p><input type="submit" value="Change"></p>
    </form>
{% endblock %}
​

​

app/templates/registration/password_change_done.html

{% extends "base.html" %}
​
{% block title %}Password changed{% endblock %}
​
{% block content %}
    <h1>Password changed</h1>
    <p>Your password has been successfully changed.</p>
{% endblock %}

感谢您的帮助!

最佳答案

我认为这里的问题是,正如 Tobit 所暗示的那样,您的 URL 正在使用一个名为 account 的应用程序命名空间。这已由您的 urls.py 中存在的 app_name = 'account' 定义。

PasswordChangeView 在查找 password_change_done View 时不需要命名空间。但是,您可以通过指定显式 success_url 属性在 urls.py 中覆盖它:

from django.urls import reverse_lazy
​
# change password urls
path('password-change/', auth_views.PasswordChangeView.as_view(success_url=reverse_lazy('account:password_change_done')), name='password_change'),

有关命名空间的更多信息:https://docs.djangoproject.com/en/2.0/topics/http/urls/#url-namespaces-and-included-urlconfs

关于python - Django 2.0 - 找不到 'password_change_done' 的反向。 'password_change_done' 不是有效的 View 函数或模式名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49683018/

相关文章:

python - Python-Django 中的图像到文本扫描

ios - 无法转换值问题 Swift iOS

python - 在 python 中创建 CSV 文件,它将列出目录中带有服务器名称的所有文件

python - 与 parent 一起杀死 child

python - 为什么 Django 让 Python 看起来很丑?

javascript - 在 OpenLayers 示例中调用 domtoimage.toPng() 时出现 "SecurityError: This operation is insecure"

ruby-on-rails - 阻止访客再次投票

java - 如何使用java代码登录网站?

Python3 : Matching elements between two lists based upon substrings

python 排序列表列表与决胜局