comment – Django template tags

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow paassing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments etc.
This article revolves about how to use comment tag in Template. Template ignores everything between {% comment %} and {% endcomment %}. An optional note may be inserted in the first tag. For example, this is useful when commenting out code for documenting why the code was disabled.

Syntax
{% commment 'comment_name' %}
{% endcomment %}
Example
{% comment "Optional note" %}
    

Commented out text with {{ create_date|date:"c" }}

{% endcomment %}

Comment – Django template Tags Explanation

Illustration of How to use comment tag in Django templates using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.

Now create a view through which we will pass the context dictionary,
In geeks/views.py,



filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
    # create a dictionary
    context = {
        "data" : "<h1>GeeksForGeeks is the Best</h1>",
    }
    # return response
    return render(request, "geeks.html", context)

chevron_right


Create a url path to map to this view. In geeks/urls.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.urls import path
  
# importing views from views..py
from .views import geeks_view
  
urlpatterns = [
    path('', geeks_view),
]

chevron_right


Create a template in templates/geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

Data uncommented :
{{ data }}
Data commented :
{% comment "Optional note" %}
    {{ data }}
{% endcomment %}

chevron_right


Let’s check is comments are displayed in the template.
comment-django-template-tags




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.