我的观点部分:
@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 docs。我试图在我自己的应用程序中实现它们。一切正常。我已经创建了自己的登录和注销视图。但是当我添加'password_change'
和'password_change_done'
次观看时,它会抛出异常。
我认为'password_change_done'
视图无法在我的项目级别网址中使用'namespacing'
来解析网址。如何强制使用我的命名空间,即'student'
?
请!帮助我纠正它......
谢谢!提前...
答案 0 :(得分:0)
您应该能够使用current_app
参数告知网址解析程序您的观看位置。
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'),