79365925

Date: 2025-01-17 19:38:21
Score: 1.5
Natty:
Report link

Be careful with HTML Comments with Unclosed Tags.

Go to a Django project and write this line in a template

<!-- {% if context %} -->

What happens is that Django will raise this error:

TemplateSyntaxError: Unclosed tag on line XXX: 'if'. Looking for one of: elif, else, endif.

Wait, what? Why is a comment raising an error? Aren't comments ignored? Well... yes and no.*


Why is this happening?


If you close the tag, no error happens.

<!-- {% if context %} {% endif %} -->


What to do if I want to make a comment with an unclosed tag?

Use Django's Template comments and not html's. Django template's comments are ignored and aren't passed to the browser (the yes part).*

You have two options:

  1. Use comment syntax to comment-out part of a line in a template:
{# {% if context %} #}
  1. Use the comment tag for multiline comment, it ignores everything between them.
{% comment %}  {% if context %} {% endcomment %}
Reasons:
  • Blacklisted phrase (1): What to do if
  • Blacklisted phrase (1): to comment
  • RegEx Blacklisted phrase (1): I want
  • RegEx Blacklisted phrase (0.5): Why is this
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • High reputation (-1):
Posted by: Guzman Ojero