如何在Django模板中添加注释


202

我想用一行评论

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



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

Answers:


310

作为Miles的答案,{% comment %}...{% endcomment %}用于多行注释,但您也可以像这样在同一行上注释掉文本:

{# some text #}

11
的确如此,但是如果您有{% extends "file.html" %}标记,则应将其放在模板文件的最上方,甚至在{% comment %}... 之前{% endcomment %},否则会出现<ExtendsNode: extends "file.html"> must be the first tag in the template错误。我的意思是,如果有人想将多行注释放在模板顶部。
pebox11


27

使用{# #}表示法,如下所示:

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

10

与传统的html注释相反:

<!-- not so secret secrets -->

Django模板注释未在最终的html中呈现。因此,您可以随意在其中填充实现细节,如下所示:

多行:

{% comment %}
    The other half of the flexbox is defined 
    in a different file `sidebar.html`
    as <div id="sidebar-main">.
{% endcomment %}

单线:

{# jquery latest #}

{#
    beware, this won't be commented out... 
    actually renders as regular body text on the page
#}

我发现这对于<a href="{% url 'view_name' %}"尚未创建的视图特别有用。


3

django模板中的多行注释使用如下,例如:.html等。

{% comment %} All inside this tags are treated as comment {% endcomment %}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.