1

在我的“根”模板中,我有这样的东西

{% if special %}
some_special_html
{% endif %}

special模板变量被某些视图插入到模板中。

问题是我需要password_change视图来设置special模板变量。

解决这个问题的最佳方法是什么?

目前,password_change视图是直接从 urls.py 调用的:

url(r'^change_password/$', 'django.contrib.auth.views.password_change',
    {'template_name': 'profile/password_change.html'},
    name='password_change'),
4

2 回答 2

3

至少从 Django 1.3 开始,password_change视图确实需要额外的上下文,尽管文档没有提到它。

kwargs您可以使用函数的参数将额外的关键字参数传递给视图url,因此要获取额外的上下文,请执行以下操作:

url(r'^password/change/$',
    auth_views.password_change,
    {'template_name': 'profile/password_change.html'},
    name='password_change',
    kwargs=dict(extra_context={'special': 'special'}),
    ),
于 2011-08-25T01:45:46.713 回答
0

要么将你对specialvar 的处理移动到 context_processor 中,要么只是password_change用你自己的视图包装 auth 视图,该视图在正确的上下文中传递。

于 2010-11-13T19:56:31.677 回答