如何在 URL 处理程序中将 extra_context 传递给 auth_views.password_change
我有一个用于更改密码的 URL 处理程序,如下所示:
url(r'^password/change/$',
auth_views.password_change,
{'template_name' : 'account/password/password_change_form.html'},
name='auth_password_change'),
一切都很好,并且我的模板已加载,并且密码更改按预期进行。但是,我的模板需要访问 request.user
。默认情况下,request
对象不会从内置的 auth_views.password_change
视图传递给模板。
我知道 auth_views.password_change 视图可以在额外的上下文中传递,它将传递给模板。我只是不知道如何在我的 URL 处理程序中执行此操作。
我知道我可以编写另一个包含 auth_views.password_change
的视图,但我很好奇是否有一种快捷方式可以从 URL 处理程序中执行此操作。
您能否向我展示如何修改当前的 URL 处理程序以将当前 request
对象作为 extra_context
传递到 auth_views.password_change
视图?
I have a URL handler for changing passwords, such as this:
url(r'^password/change/
All is good, and my template is loaded, and password changing works as expected. However, my template needs to access request.user
. The request
object is not passed by default to the template from the built-in the auth_views.password_change
view.
I know that the auth_views.password_change
view can be passed in extra context, which it will pass along to the template. I just do not know how to do it in my URL handler.
I know that I could just write another view which wraps around auth_views.password_change
, but I am curious if there is a shortcut way to do it from within the URL handler.
Can you show me how to modify my current URL handler to pass in the current request
object as extra_context
to the auth_views.password_change
view?
,
auth_views.password_change,
{'template_name' : 'account/password/password_change_form.html'},
name='auth_password_change'),
All is good, and my template is loaded, and password changing works as expected. However, my template needs to access request.user
. The request
object is not passed by default to the template from the built-in the auth_views.password_change
view.
I know that the auth_views.password_change
view can be passed in extra context, which it will pass along to the template. I just do not know how to do it in my URL handler.
I know that I could just write another view which wraps around auth_views.password_change
, but I am curious if there is a shortcut way to do it from within the URL handler.
Can you show me how to modify my current URL handler to pass in the current request
object as extra_context
to the auth_views.password_change
view?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常您可以在 urlconf 中执行此操作 - 但您无法在 extra_context 中传递
request.user
,因为 urls.py 无权访问该请求。但是
password_change
与所有内置视图一样,使用RequestContext
来呈现模板 - 这意味着那里自动有一个user
对象(通过由django.contrib.auth.context_processors.auth
添加,除非您已将其从settings.py中的CONTEXT_PROCESSORS
列表中删除)。Normally you do that in the urlconf - but you can't pass
request.user
in extra_context, because the urls.py has no access to the request.But
password_change
, like all built-in views, usesRequestContext
to render the template - which means that there is auser
object there automatically (passed in bydjango.contrib.auth.context_processors.auth
, unless you've removed it from theCONTEXT_PROCESSORS
list in settings.py).