问题
I have the following url config in accounts/urls.py:
url(r'^password/change/$', 'django.contrib.auth.views.password_change',
{'template_name': 'change_password.html'}, name='password_change'),
url(r'^password/change/done/$', 'django.contrib.auth.views.logout_then_login',
name='password_change_done'),
But if I call the password-change url I get the following
error: NoReverseMatch at /accounts/password/change/ Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
If the user modified his password, it is to be logged out and login again.
Thanks a lot.
edit, traceback
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/password/change/
Django Version: 1.7
Python Version: 3.4.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'widget_tweaks',
'djangular',
'djcelery',
'accounts',
'system_utils',
'alert_system',
'proj',
'django_cleanup')
Installed Middleware:
('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/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/views/decorators/debug.py" in sensitive_post_parameters_wrapper
76. return view(request, *args, **kwargs)
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/contrib/auth/views.py" in password_change
261. post_change_redirect = reverse('password_change_done')
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/core/urlresolvers.py" in reverse
546. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/peter/Projekte/proj-hoster/proj-hoster-env3/lib/python3.4/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
463. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /accounts/password/change/
Exception Value: Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
回答1:
One of the references to 'password-change-done' has dashes, the other has underscores. It's likely something to do with that!
回答2:
I realize that this is a very old question, but I had the same error yesterday. I think the namespace is the problem. Once you've namespaced the urls, they'll all need to be prefixed with the name of the namespace (in your case "accounts") and a colon. If you look at the code for the password_change
view, it (conditionally) redirects based on a url of 'password_change_done'. Because that url hasn't been decorated with your namespace, it can't be found. Drop the namespace, ensure that your url references no longer have it and you should be OK.
来源:https://stackoverflow.com/questions/26196678/django-change-password-noreversematch-at-accounts-password-change