如何在 URL 处理程序中将 extra_context 传递给 auth_views.password_change

发布于 2024-12-13 01:46:44 字数 800 浏览 0 评论 0原文

我有一个用于更改密码的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-12-20 01:46:44

通常您可以在 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, uses RequestContext to render the template - which means that there is a user object there automatically (passed in by django.contrib.auth.context_processors.auth, unless you've removed it from the CONTEXT_PROCESSORS list in settings.py).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文