Django 尝试呈现甚至注释掉的 HTML 代码,这会导致错误

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

渲染我的 django 项目的页面时,我收到以下错误,即使 HTML 代码的关键部分被注释掉了:

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

[...]

有问题的注释掉的代码如下:

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

我想保留它,以防我需要提醒自己如何将 URL 发送到我的 django 项目中的另一个应用程序,在本例中称为 'blog'。为什么不能把它注释掉,留在那里让它被渲染过程忽略?

wrapping it as php-code都没用:

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

Django 模板渲染不关心 HTML,它关心 {% %},它不是 PHP。所以将它包含在 PHP 块中也不会做任何事情。

How to put comments in Django templates

有答案,但“{# comment #}”在你的街区。

所以

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