NoReverseMatch at / Reverse for 'password_chan

2019-08-14 20:59发布

问题:

my views part:

@login_required
def password_change(request, 
        template_name='register.html',
        password_change_form=PasswordChangeForm,):
    if post_change_redirect is None:
        post_change_redirect = reverse('student:login')
    else:
        post_change_redirect = reverse_url(post_change_redirect)
    if request.method == 'POST':
        form = password_change_form(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            update_session_auth_hash(request, form.user)
            return HttpResponseRedirect(post_change_redirect)
    else:
        form = password_change_form(user=request.user)
    context = {
        'form': form,
        'title': _('Password change'),
    }   
    return TemplateResponse(request, template_name, context)

@login_required
def password_change_done(request, 
    template_name='password_change_done.html',):

    context = {
        'title': _('Password change successful'),
    }
    return TemplateResponse(request, template_name, context)

my project urls:

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url(r'^stu/', include('student.urls', namespace='student')),
    url(r'^admin/', include(admin.site.urls)),
]

corresponding app urls:

from django.conf.urls import url, patterns
from django.contrib.auth import views as auth_views
from . import views

urlpatterns = [
    url(r'^register/$', views.register, name='register'),
    url(r'^login/$', views.user_login, name='login'),
    url(r'^logout/$', views.user_logout, name='logout'),
    url(r'^change-password/$', auth_views.password_change, name='password_reset'),
    url(r'^password-change-done/$', views.password_change_done, name='password_change_done'),
    url(r'^restricted/', views.restricted, name='restricted'),
    url(r'^mains/', views.mains, name = 'mains'),
]

my traceback:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/stu/change-password/

Django Version: 1.8.1
Python Version: 3.4.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'student')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware')


Traceback:
File "C:\Python34\lib\site-packages\django\core\handlers\base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django\views\decorators\debug.py" in sensitive_post_parameters_wrapper
  76.             return view(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\utils\decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "C:\Python34\lib\site-packages\django\contrib\auth\views.py" in password_change
  293.         post_change_redirect = reverse('password_change_done')
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in reverse
  579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python34\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  496.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /stu/change-password/
Exception Value: Reverse for 'password_change_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

I'm following django-auth docs. I'm trying to implement them in my own app. Everything works fine. I've made my own login and logout views. But when I add 'password_change' and 'password_change_done' views it throws above exception.

I think that 'password_change_done' view didn't be able to resolve url as I've using 'namespacing' in my project level url's. How can I enforce it to use my namespacing i.e, 'student'?

Please! help me to make it correct....

Thanks! in Advance...

回答1:

You should be able to use the current_app parameter to tell the URL resolver where your views are.

url(r'^change-password/$', auth_views.password_change, {'current_app': 'student'}, name='password_reset'),
url(r'^password-change-done/$', views.password_change_done,  {'current_app': 'student'}, name='password_change_done'),