【问题标题】:如何在 Django 模板中添加注释
【发布时间】:2010-10-17 17:19:34
【问题描述】:

我想用一行来评论这个

{% if something.property %}
    <table>
        <tr>...



{% # this is a comment %}
{% if something.property %}
    <table>
        <tr>...

【问题讨论】:

    标签: django django-templates


    【解决方案1】:

    评论标签记录在https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatetag-comment

    {% comment %} this is a comment {% endcomment %}
    

    单行 cmets 记录在 https://docs.djangoproject.com/en/stable/topics/templates/#comments

    {# this won't be rendered #}
    

    【讨论】:

      【解决方案2】:

      正如 Miles 的回答,{% comment %}...{% endcomment %} 用于多行 cmets,但您也可以像这样在同一行注释掉文本:

      {# some text #}
      

      【讨论】:

      • 没错,但如果你有一个{% extends "file.html" %} 标签,你应该把它放在模板文件的最顶部,甚至在{% comment %}...{% endcomment %} 之前,否则你会得到一个&lt;ExtendsNode: extends "file.html"&gt; must be the first tag in the template 错误。我是说如果有人想将多行 cmets 放在模板的顶部。
      【解决方案3】:

      使用{# #} 符号,如下所示:

      {# Everything you see here is a comment. It won't show up in the HTML output. #}
      

      【讨论】:

        【解决方案4】:

        如果你想注释一些 Django 模板格式的代码,这种方式会很有帮助。

        {#% include 'file.html' %#}(正道)

        如果使用 HTML 注释进行注释,以下代码仍会执行。

        &lt;!-- {% include 'file.html' %} --&gt;(走错路了)

        【讨论】:

          猜你喜欢
          • 2014-05-10
          • 1970-01-01
          • 2012-03-17
          • 1970-01-01
          • 2019-07-02
          • 1970-01-01
          • 2019-02-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多