如何将变量从包含的模板传递到包含它的模板? [英] how to pass the variable from included template to the template where it is included?
问题描述
在 Django 视图中:-
if request.is_ajax():t = get_template('bar-templates.html')html = t.render(Context({'edit': True, 'user':'some-user' }))返回 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:
{% 扩展 "base.html" %}<div class="容器">{{ edit }}//这里我没有得到编辑值{% if edit %}//我需要这个编辑值.但是这个值与 `bar-templates.html` 相关做一点事{% 别的 %}做一点事{% 万一 %}<div class="内容">{% 包含 "bar-templates.html" %}
bar-templaes.html
{{ edit }}//这里给了我编辑值这是我从视图中发送的模板.
如何将 included template
变量值用于包含它的模板.
使用您其他帖子中的详细信息,您应该对其进行编辑以包含在此帖子中:
据我所知,您正在尝试将选定"类添加到侧边栏菜单中.这对您不起作用,因为正如 gcbirzan 所说,您将无法将 ajax 响应的上下文添加到您的基本模板中.最重要的是,您不会重新渲染基本模板,因此无论如何它都不会改变.
使用 javascript,您可以从 part.html
中提取 foo_id.由于该代码未显示,假设您将 foo_id 放在隐藏的 div 中,<div id="foo_id" style="display:none;">foo_2</div>
现在您可以将 ajax 函数更改为如下所示:
$.ajax({类型:'GET',缓存:'假',网址:"/foobar/",成功:功能(数据){$('#main-content').html(data);var $foo_id = $('#foo_id').val();$('#foo1>ul>li.selected').removeClass('selected');$('#'+ foo_id).addClass('selected');}});
In Django Views:-
if request.is_ajax():
t = get_template('bar-templates.html')
html = t.render(Context({'edit': True, 'user':'some-user' }))
return HttpResponse(html)
There is two templates:
Main template (foo-templates.html)
which includes the template (bar-templates.html)
. In context edit
and user
is passed to the bar-templates.html
But this variable is also used in foo-templates.html
. In django we used to {{ edit }}
to catch the variable. Since this variable comes in bar-templates.html
. How can I use this to 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 }} // Here It gives me the edit value This is the templates which I am sending from views.
How to use the included template
variable values to the template where it get included.
Using details from your other post, which you should edit to include into this one:
From what i can tell, you are trying to add a "selected" class to your sidebar menu. This won't work for you because as gcbirzan said, you aren't going to be able to get the context of your ajax response into your base template. On top of that you aren't going to re-render the base template so it wouldn't be changing anyways.
Using javascript you can extract the foo_id from your part.html
. Since that code isn't shown, lets say you have your foo_id in a hidden div, <div id="foo_id" style="display:none;">foo_2</div>
Now you can change your ajax function to something like this:
$.ajax({
type:'GET',
cache: 'false',
url:"/foobar/",
success:function(data) {
$('#main-content').html(data);
var $foo_id = $('#foo_id').val();
$('#foo1>ul>li.selected').removeClass('selected');
$('#'+ foo_id).addClass('selected');
}
});
这篇关于如何将变量从包含的模板传递到包含它的模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!