【问题标题】:what happen with this django-template if tag?这个 django-template if 标签会发生什么?
【发布时间】:2010-12-02 03:55:12
【问题描述】:

对不起,如果我是个白痴,但是这个 if 标签会发生什么?

查看返回:

return render_to_response('productos/por_estado.html', {'productos':productos}, 
                   context_instance=RequestContext(request))
#Im not returning 'estado' !

模板:

{% if estado %}
{% block activos_active %}class="active"{% endblock %}
{% endif %}

模板html结果:

class="active"

:S

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    块标签不能有条件地设置,可以说是因为它们是模板继承的一部分。我相信这是 Django 开发人员明确的设计决定。请参阅 Stack Overflow 上的 this Django ticket 和 this other question。

    【讨论】:

    • 所以我必须做一些棘手的事情来设置“class="active"”的另一种方式。感谢 4 您的快速回复。
    【解决方案2】:

    设置class="active" 的一种方法可能就是这样

    <div{% if estado %} class="active"{% endif %}>....</div>
    

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2014-06-14
      • 2022-01-24
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      相关资源
      最近更新 更多