Chapter 5: Django comment Tag

comment Tag: {% comment %} and its friends — the proper way to write comments inside templates.

Many beginners either:

  • use HTML comments <!– –> (which appear in source code — bad for security & debug info)
  • or they don’t comment at all and come back 6 months later wondering “what the hell was I thinking here?”

Today I’m going to teach you every realistic way to use Django’s comment tag — like a senior who has written 10,000+ lines of Django templates explaining it to a junior.

We’ll use your polls templates as playground.

1. The Basic {% comment %} … {% endcomment %} Block

This is the official, safe, Django-recommended way to comment out code in templates.

HTML

Why this is better than HTML comment <!– –>

  • {% comment %} content never reaches the browser — it’s removed during template rendering
  • HTML comments <!– –> are sent to the client → can leak debug info, internal notes, or even sensitive logic
  • Django comments can be multi-line and nested (more on nesting later)

2. One-Line Comment (Most Common in Practice)

HTML

Pro tip — many teams use short {% comment %} lines right above tricky logic:

HTML

3. Commenting Out Large Blocks (Debugging / Development)

Very useful when testing:

HTML

→ The whole form disappears from output — perfect for isolating bugs

4. Nesting Comments (Allowed & Useful)

HTML

Django fully supports nested {% comment %} blocks — very handy when commenting large legacy code.

5. When NOT to Use {% comment %} (Important!)

Do not use it for:

  • Template logic documentation that should be visible to designers/front-end devs → Use regular HTML comments <!– note for designer –> or {# note #} (short form)

Short one-line alternative (less used):

HTML

→ {# … #} is the Jinja-style short comment — works in Django too

6. Real-World Examples from Your Polls App

Put these in index.html or detail.html

Example 1: Comment why we filter is_active

HTML

Example 2: Comment temporary debug code

HTML

Example 3: Comment old layout variant

HTML

Example 4: Comment TODOs & future plans

HTML

7. Common Beginner Mistakes & Fixes

Mistake What happens Fix / Correct way
Put comment before {% extends %} TemplateSyntaxError or blank page {% extends %} must be first thing in file
Use HTML <!– –> for internal notes Comments visible in source code Use {% comment %} for anything not meant for browser
Forget {% endcomment %} Syntax error or rest of template ignored Always close the block
Use {# #} for multi-line Only first line commented Use {% comment %} … {% endcomment %} for multi-line

Quick Reference – When to Use Which Comment Style

text

Your Quick Practice Task (Do This Right Now)

Open polls/templates/polls/index.html or detail.html and add these:

  1. Above the question loop → {% comment %} Showing only active polls – inactive kept for admin review {% endcomment %}
  2. Inside loop → {# TODO: add trending badge if votes > 20 #}
  3. Comment out one <div> temporarily → {% comment %}<div>Old debug</div>{% endcomment %}
  4. Add {% empty %} message with comment explaining why it exists

Tell me what you want next:

  • Which comment style still feels awkward?
  • Want to see how to use comments with {% include %} or {% block %}
  • Ready to learn other less-used tags ({% verbatim %}, {% spaceless %}, {% widthratio %})?
  • Or ready to move to Django Forms + Voting + POST handling?

You’re now using template comments like a pro — small detail, big difference in maintainable code.

Keep going — you’re doing really well! 🚀🇮🇳

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *