【发布时间】:2011-07-29 07:38:19
【问题描述】:
在 Django 视图中:-
if request.is_ajax():
t = get_template('bar-templates.html')
html = t.render(Context({'edit': True, 'user':'some-user' }))
return HttpResponse(html)
有两个模板:
主模板(foo-templates.html),其中包括模板(bar-templates.html)。在上下文中edit 和user 被传递给bar-templates.html 但这个变量也在foo-templates.html 中使用。在 django 中,我们使用{{ edit }} 来捕获变量。因为这个变量来自bar-templates.html。我怎样才能用这个来foo-templates.html。
foo-templates.html:
{% extends "base.html" %}
<div class="container">
{{ edit }} // Here I am not getting the edit value
{% if edit %} // I need this edit value. But this value is in context with `bar-templates.html`
do something
{% else %}
do something
{% endif %}
<div class="content">
{% include "bar-templates.html" %}
</div>
bar-templaes.html
{{ edit }} // 这里给了我编辑值 这是我从视图发送的模板。
如何将included template 变量值用于包含它的模板。
【问题讨论】:
标签: python django django-templates