我想在这里添加一行注释:
{% if something.property %}
<table>
<tr>...
{% # this is a comment %}
{% if something.property %}
<table>
<tr>...
我想在这里添加一行注释:
{% if something.property %}
<table>
<tr>...
{% # this is a comment %}
{% if something.property %}
<table>
<tr>...
如Miles所回答的那样,{% comment %}...{% endcomment %}
用于多行注释,但您也可以像这样在同一行上注释文本:
{# some text #}
注释标签的文档在 https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatetag-comment。
{% comment %} this is a comment {% endcomment %}
单行注释的文档在https://docs.djangoproject.com/en/stable/topics/templates/#comments中有说明。
{# this won't be rendered #}
使用{# #}
表达式,如下所示:
{# Everything you see here is a comment. It won't show up in the HTML output. #}
如果你想要对 Django 模板格式代码进行评论,这种方式可能会有所帮助。
{#% include 'file.html' %#}
(正确的方式)
即使使用 HTML 注释对以下代码进行注释,它仍然会执行。
<!-- {% include 'file.html' %} -->
(错误的方式)
如果你想在{% extends ... %}
之前添加注释,那么这种方法行不通。在这种情况下,最好使用
<!--
# comment 1
# comment 2
# comment 3
-->
{% extends "file.html" %}
标签,你应该将它放在模板文件的最顶部,甚至在{% comment %}
...{% endcomment %}
之前,否则你会得到一个<ExtendsNode: extends "file.html"> must be the first tag in the template
错误。我是在提醒那些想要把多行注释放在模板顶部的人。 - pebox11