对于'password_change_done','()'和关键字参数'{}'找不到。尝试了 0 种模式: []



我在Django中遇到以下错误。与参数'()'的" password_change_done"相反,找不到找到关键字参数'{}'。0模式尝试:[]。我不太确定为什么我会遇到这个错误。

urls.py

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
urlpatterns = [
    url(r'^login/$', auth_views.login, name='login'),
    url(r'^logout/$', auth_views.logout, name='logout'),
    url(r'^logout-then-login/$', auth_views.logout_then_login, name="logout_then_login"),
    url(r'^$', views.dashboard, name='dashboard'),
    #change password urls
    url(r'^password-change/$', auth_views.password_change, name='password_change'),
    url(r'^password-change/done/$', auth_views.password_change_done, name='password_change_done'),
]

追溯: 环境:

Request Method: GET
Request URL: http://127.0.0.1:8000/account/password-change/
Django Version: 1.10.4
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'whitenoise',
 'crispy_forms',
 'business',
 'account']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
  149.                     response = view_func(request, *args, **kwargs)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  23.                 return view_func(request, *args, **kwargs)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/contrib/auth/views.py" in inner
  47.         return func(*args, **kwargs)
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/contrib/auth/views.py" in password_change
  308.         post_change_redirect = reverse('password_change_done')
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/urls/base.py" in reverse
  91.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/Users/Tommy/Desktop/django/directory/lib/python3.5/site-packages/django/urls/resolvers.py" in _reverse_with_prefix
  392.             (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /account/password-change/
Exception Value: Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

html页面我正在尝试打开

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block title %}Change Your Password{% endblock %}
{% block content %}
<div class="offset-md-3 col-md-6">
    <h1>Change Your Password</h1>
    <p>Use the form below to change your password.</p>
    <form action="." method="post">
        {{form|crispy}}
        {% csrf_token %}
    </form>
</div>
{% endblock %}

由于您在应用程序的url.py中添加password_change,因此应该使用post_change_redirect参数指定POST_CHANGE URL,包括应用程序名称:

url(r'^password-change/$', password_change, {'post_change_redirect': 'account:password_change_done'}, name='password_change'),
url(r'^password-change/done/$', password_change_done, name='password_change_done'),

其中account是应用程序URL的名称空间。

问题可能是您的URL被命名为空间,而您忘记使用了名称空间。尝试:

{% url 'account:password_change_done' %}

reverse('account:password_change_done')

记住要命名您的名字,否则Django可能找不到它们。您可以通过查看包含登录URL等的位置来找到所使用的名称空间(如果不是account)。它应该看起来像:

url(r'account/', include('account.urls', namespace='account')),

最新更新