【问题标题】:Django: outcommenting in templatesDjango:模板中的注释
【发布时间】:2009-06-22 10:15:08
【问题描述】:

如何取消注释或禁用模板中的行(不删除行)?

例如如果文件“base_weblog.html”包含:

{% load ProgramVersion %}{% render_month_links %}

如何在运行时隐藏这条线?

这不起作用(例如,如果 ProgramVersion 不是有效的标签库,则 TemplateSyntaxError - 这就是我要评论的原因):

{% 如果为假 %}

{% load ProgramVersion %}{% render_month_links %}

{% endif %}

更新 1。这样就解决了:

{% 评论 %}

{% load ProgramVersion %}{% render_month_links %}

{% endcomment %}

只是好奇:为什么在第一种情况下评估“load ProgramVersion”而不在第二种情况下评估?优化可能嵌套的控制结构是否太复杂(并且 cmets 不能嵌套)? 请注意,上面的“if false”应该是“if False”,但这没有区别。和 一个不存在的变量,比如 XYZ,负载仍然被评估。

【问题讨论】:

    标签: django


    【解决方案1】:

    试试:

    {% comment %} .... {% endcomment %}
    

    咨询Django template documentation

    【讨论】:

      【解决方案2】:

      这样声明的评论不会向用户显示。

      {% comment %} 你的评论文本 {% endcomment %}

      【讨论】:

        【解决方案3】:

        cmets 还有第二个选项。很适合单排。

        {# {% load ProgramVersion %}{% render_month_links %} #}
        

        【讨论】:

          【解决方案4】:

          你可以这样做:

          {% comment %} 'this will not be displayed' {% endcomment %}
          

          {# {{ form.as_p }} #}
          

          注意:您不能这样做来注释掉多行代码:

          {# <h1>Some header</h1>
          {{ form.as_p }}
           #}
          

          您要注释掉的每一行代码都应该用这个 {# #} 括起来

          {# <h1>Some header</h1> #}
          {# {{ form.as_p }} #}
          

          【讨论】:

            猜你喜欢
            • 2012-03-17
            • 2019-02-19
            • 1970-01-01
            • 2014-08-12
            • 2010-10-17
            • 2018-11-25
            • 1970-01-01
            • 2018-04-28
            • 2014-10-19
            相关资源
            最近更新 更多