To comment out a block of code in a Django template, use the {% comment %} and {% endcomment %} tags and pass the code to not render between the tags.
{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.
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 #}
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 #}
Using the {# #}
notation, like so:
{# Everything you see here is a comment. It won't show up in the HTML output. #}
In contrast to traditional html comments like this:
<!-- not so secret secrets -->
Django template comments are not rendered in the final html. So you can feel free to stuff implementation details in there like so:
Multi-line:
{% comment %}
The other half of the flexbox is defined
in a different file `sidebar.html`
as <div id="sidebar-main">.
{% endcomment %}
Single line:
{# jquery latest #}
{#
beware, this won't be commented out...
actually renders as regular body text on the page
#}
I find single line comments especially helpful as placeholders for views that have not been created yet <a href="{% url 'view_name' %}"
.
Multiline comment in django templates use as follows ex: for .html etc.
{% comment %} All inside this tags are treated as comment {% endcomment %}
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With