ChatGPT解决这个技术问题 Extra ChatGPT

How to put comments in Django templates

I would like to comment this with a line

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



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

V
Van Gale

As answer by Miles, {% comment %}...{% endcomment %} is used for multi-line comments, but you can also comment out text on the same line like this:

{# some text #}

True, but if you have an {% extends "file.html" %} tag you should put that on the very top of the template file even before the {% comment %}...{% endcomment %}, otherwise you'll get an <ExtendsNode: extends "file.html"> must be the first tag in the template error. I am saying that in case someone wants to place the multi-line comments on the top of the template.
M
Miles

Comment tags are documented at https://docs.djangoproject.com/en/stable/ref/templates/builtins/#std:templatetag-comment

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

Single line comments are documented at https://docs.djangoproject.com/en/stable/topics/templates/#comments

{# this won't be rendered #}

m
mipadi

Using the {# #} notation, like so:

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

R
Rahul Shyokand

This way can be helpful if you want to comment some Django Template format Code.

{#% include 'file.html' %#} (Right Way)

Following code still executes if commented with HTML Comment.

<!-- {% include 'file.html' %} --> (Wrong Way)


s
stats con chris

this doesn't work if you want to comment before {% extends ... %} In this case better use

<!--
# comment 1
# comment 2
# comment 3
-->