NoReverseMatch at / Reverse for 'password_change_done' 与参数 '()' 和关键字参数 '' 未找到。尝试了 0 种模式:[]

Posted

技术标签:

【中文标题】NoReverseMatch at / Reverse for \'password_change_done\' 与参数 \'()\' 和关键字参数 \'\' 未找到。尝试了 0 种模式:[]【英文标题】:NoReverseMatch at / Reverse for 'password_change_done' with arguments '()' and keyword arguments '' not found. 0 pattern(s) tried: []NoReverseMatch at / Reverse for 'password_change_done' 与参数 '()' 和关键字参数 '' 未找到。尝试了 0 种模式:[] 【发布时间】:2015-10-15 01:18:09 【问题描述】:

我的观点部分:

@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)

我的项目网址:

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)),
]

对应的应用网址:

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'),
]

我的回溯:

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: []

我正在关注 django-auth 文档。我正在尝试在我自己的应用程序中实现它们。一切正常。我制作了自己的登录和注销视图。但是当我添加 'password_change''password_change_done' 视图时,它会抛出异常。

我认为'password_change_done' 视图无法解析网址,因为我在项目级网址中使用了'namespacing'。如何强制它使用我的命名空间,即'student'

请!帮我改正....

谢谢!提前...

【问题讨论】:

【参考方案1】:

您应该能够使用current_app 参数告诉 URL 解析器您的视图在哪里。

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'),

【讨论】:

这似乎是deprecated since Django 1.9 and will be removed in 2.0

以上是关于NoReverseMatch at / Reverse for 'password_change_done' 与参数 '()' 和关键字参数 '' 未找到。尝试了 0 种模式:[]的主要内容,如果未能解决你的问题,请参考以下文章

NoReverseMatch at ...... in django

NoReverseMatch at / posts / post / 18 / comment / Django Error

Django NoReverseMatch at / 路径名和/或顺序是不是冲突?

NoReverseMatch at / Reverse for 'post_detail' 未找到

NoReverseMatch at /accounts/password-reset/ 用于 Django 中的密码重置

NoReverseMatch at / Reverse for 'singlepropa' 与参数 '('mens-shirt-1',)' 未找到。已尝试 1 种模式