我在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 for page我试图打开
{% 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 %}
答案 0 :(得分:2)
由于您在应用程序password_change
中添加url.py
,您应该使用post_change_redirect
参数指定包含应用程序名称的post_change网址:
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
是应用程序网址的命名空间。
答案 1 :(得分:0)
问题可能是您的网址已命名,而您忘记使用命名空间。尝试:
{% url 'account:password_change_done' %}
或
reverse('account:password_change_done')
请记住命名您的名字,否则django可能无法找到它们。您可以通过查看包含登录网址的位置等来找到您正在使用的命名空间(如果它不是account
)。它应该类似于:
url(r'account/', include('account.urls', namespace='account')),