How to Comment in Django Templates

In many cases, it makes more sense to comment out code in Django templates so it does not render on the front-end in templates.

 

In this tutorial, we will learn how to make single line Django Template comments for annotation purposes and comment out blocks of code.

 

Single Line Django Comment

To make a single-line comment in a Django template use the {##} tag and pass the comment between the two hashes. Here is an example of a code annotation:

 

example.html
{# The code below is is for... #}

 

It is also possible to comment out single lines of Django template tags and HTML using this method:

 

example.html
{# {% block content %} #}

{# <div class="w-full">°</div> #}

 

Django Comment Blocks

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.

 

example.html
{% comment %} this is a comment {% endcomment %}

 

 

And another example of commenting out some code.

 

example.html
{% comment %}

{% extends 'layouts/base.html' %}

{% block content %}
  <div class="w-full"></div>
{% endblock %}

{% endcomment %}