Comments in Django: be careful

HTML comments are not ignored by the Django parser.

If there is an error inside the html comment, an error will still be raised.

For example, you have an url tag directed to a non existent URL name:

<!–<a href=”{% url ‘prova’ %}”&gt…


This content originally appeared on DEV Community and was authored by guzmanojero

HTML comments are not ignored by the Django parser.

If there is an error inside the html comment, an error will still be raised.

For example, you have an url tag directed to a non existent URL name:

<!--<a href="{% url 'prova' %}">Prova</a>-->

Even it it's a comment a NoReverseMatch error will be raised:

NoReverseMatch: Reverse for 'prova' not found. 'prova' is not a valid view function or pattern name.

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?

  • The Django template engine parses everything, including content inside HTML comments (<!-- ... -->). So html comments aren't ignored (the no part).*

  • When it encounters {% if context %}, even if it's inside an html comment, it expects a matching {% endif %}. Since the endif is missing, an error is raised.

  • Django treats HTML comments <!-- ... --> as text, not as special syntax.

  • HTML comments are meant to be passed to the browser, although they are not displayed. Django processes their content because it doesn’t know you intend them to be ignored. Try it, make an html comment and then check the page source, html comments are there.

And 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 %}


This content originally appeared on DEV Community and was authored by guzmanojero


Print Share Comment Cite Upload Translate Updates
APA

guzmanojero | Sciencx (2025-01-17T19:30:39+00:00) Comments in Django: be careful. Retrieved from https://www.scien.cx/2025/01/17/comments-in-django-be-careful/

MLA
" » Comments in Django: be careful." guzmanojero | Sciencx - Friday January 17, 2025, https://www.scien.cx/2025/01/17/comments-in-django-be-careful/
HARVARD
guzmanojero | Sciencx Friday January 17, 2025 » Comments in Django: be careful., viewed ,<https://www.scien.cx/2025/01/17/comments-in-django-be-careful/>
VANCOUVER
guzmanojero | Sciencx - » Comments in Django: be careful. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/17/comments-in-django-be-careful/
CHICAGO
" » Comments in Django: be careful." guzmanojero | Sciencx - Accessed . https://www.scien.cx/2025/01/17/comments-in-django-be-careful/
IEEE
" » Comments in Django: be careful." guzmanojero | Sciencx [Online]. Available: https://www.scien.cx/2025/01/17/comments-in-django-be-careful/. [Accessed: ]
rf:citation
» Comments in Django: be careful | guzmanojero | Sciencx | https://www.scien.cx/2025/01/17/comments-in-django-be-careful/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.