2011-04-04 100 views
0

在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)。在上下文中edituser传递给bar-templates.html但是这个变量也用于foo-templates.html。在Django中,我们使用{{ edit }}来捕捉变量。由于这个变量进入bar-templates.html。我怎样才能用这foo-templates.html

富-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> 

酒吧templaes.html

{{编辑}} //这里它给我的编辑值 这是模板,我从意见发送。

如何将included template变量值用于包含它的模板。

回答

0

如果你简单地渲染富:

t = get_template('foo-templates.html') 
html = t.render(Context({'edit': True, 'user':'some-user' })) 

然后“富”和所包含的“酒吧”都具有相同的价值“编辑”。

我不完全了解你的问题,但我有答案吗?

1

从您的其他职位,您应该修改要加入到这一细节使用:

从我可以告诉,你想一个“选择”类添加到您的工具栏菜单。这不适合你,因为正如gcbirzan所说,你不能将你的ajax响应的上下文放到你的基本模板中。最重要的是,你不会重新渲染基础模板,所以它不会改变。

使用JavaScript,您可以从您的part.html中提取foo_id。由于不表明代码,可以说你有一个隐藏的div你foo_id,<div id="foo_id" style="display:none;">foo_2</div>

现在你可以改变你的AJAX功能,这样的事情:

$.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'); 
    } 

}); 
相关问题