【问题标题】:Django tries to render even commented-out HTML-code which leads to errorsDjango 尝试渲染甚至被注释掉的 HTML 代码,这会导致错误
【发布时间】:2021-04-30 07:47:36
【问题描述】:

在渲染我的 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> -->
?>

【问题讨论】:

    标签: html django django-templates comments html-rendering


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2020-07-25
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      相关资源
      最近更新 更多