htmldjangodjango-templatescommentshtml-rendering

Django tries to render even commented-out HTML-code which leads to errors


When rendering a page of my django-project, I get the following error, even though the crucial part of the HTML code is commented-out:

NoReverseMatch at /current/
'blog' is not a registered namespace
Request Method: GET
Request URL:    http://localhost:8000/current/
Django Version: 3.1.5
Exception Type: NoReverseMatch
Exception Value:    
'blog' is not a registered namespace
Exception Location: /home/user/.virtualenvs/Django3_course/lib/python3.9/site-packages/django/urls/base.py, line 83, in reverse
Python Executable:  /home/user/.virtualenvs/Django3_course/bin/python
Python Version: 3.9.0
Python Path:    
['/home/user/.../todowoo_project',
 '/home/user/.../todowoo_project',
 '/home/user/Dokumente/Allgemeines_material/Sonstiges/Programming/Python/Scripts',
 '/home/user/.pyenv/versions/3.9.0/lib/python39.zip',
 '/home/user/.pyenv/versions/3.9.0/lib/python3.9',
 '/home/user/.pyenv/versions/3.9.0/lib/python3.9/lib-dynload',
 '/home/user/.virtualenvs/Django3_course/lib/python3.9/site-packages']
Server time:    Tue, 26 Jan 2021 13:56:35 +0000
Error during template rendering
In template /home/.../todo/templates/todo/base.html, error at line 97

'blog' is not a registered namespace

[...]

The commented-out code in question is the following:

<!-- <li>
    <a role="button" href="{% url 'blog:all_blogs' %}" class="btn btn-primary">Blog</a>
</li> -->

I would like to keep this in case I need to remind myself how to send an URL to another app within my django project, in this case called 'blog'. Why isn't it possible to comment it out, leave it there and let it be ignored by the rendering process?

Even wrapping it as php-code did not work:

<?php
<!-- <li>
    <a role="button" href="{% url 'blog:all_blogs' %}" class="btn btn-primary">Blog</a>
</li> -->
?>

Solution

  • Django template rendering does not care about the HTML, it cares about the {% %}, which is NOT PHP. so enclosing it in PHP blocks won't do anything either.

    How to put comments in Django templates

    has the answer, but '{# comment #}' around your block.

    so

    {% comment %}
    <li>
        <a role="button" href="{% url 'blog:all_blogs' %}" class="btn btn-primary">Blog</a>
    </li>
    {% endcomment %}