2017-09-15 31 views
0

我使用simple_tag來計算django模板中的值。我當前的代碼看起來像,與simple_tag的返回值比較

{% for param_a in params_A %} 
    {% for param_b in params_B %} 
    <p>{% awesome_tag param_a param_b %}</p> 
    # other_stuffs 
    {% endfor %} 
{% endfor %} 

現在我想跳過other_stuffs如果從awesome_tag返回的值是foo。 我知道我可以在視圖中執行此操作,然後在上下文中傳遞項目,但我想知道是否有更好的Django模板方法來執行此操作。

回答

1

您可以使用assignment_tag來實現此目的,並將返回值存儲在某個變量中並檢查模板中的變量。

{% for param_a in params_A %} 
    {% for param_b in params_B %} 
    <p>{% awesome_tag param_a param_b as result%}</p> 

    {% if result != "foo" %} 
    # other_stuffs 
    {% endif %} 

    {% endfor %} 
{% endfor %}