How to put comments in Django templates

DjangoDjango Templates

Django Problem Overview


I would like to comment this with a line

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



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

Django Solutions


Solution 1 - Django

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 #}

Solution 2 - Django

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 #}

Solution 3 - Django

Using the {# #} notation, like so:

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

Solution 4 - Django

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)

Solution 5 - Django

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex. S.View Question on Stackoverflow
Solution 1 - DjangoVan GaleView Answer on Stackoverflow
Solution 2 - DjangoMilesView Answer on Stackoverflow
Solution 3 - DjangomipadiView Answer on Stackoverflow
Solution 4 - DjangoRahul ShyokandView Answer on Stackoverflow
Solution 5 - Djangostats con chrisView Answer on Stackoverflow