【问题标题】:a strange bug on template's url tag error模板的 url 标签错误上的一个奇怪错误
【发布时间】:2013-05-17 11:56:42
【问题描述】:

我正在使用 django 1.4.5。

在我的模板中,我包含了{% load url from future %},并遵循了相应地引用网址的标准模式。

比如说,在`urls.py'中:

url(
    regex = r'^$',
    view = MyClassBasedView.as_view(),
    name = 'index'),

在模板内部:

<a href="{% url "index" %}">back to index</a>

但我还是觉得那个讨厌的syntax error

**Error during template rendering:**
73 <a href="{% url "index" %}">back to index</a>

你知道我为什么会收到这个错误吗?我错过了什么吗?

【问题讨论】:

  • index周围尝试单引号
  • 我做了..但没用..
  • 您确定错误与url 标签有关吗?对我来说看起来很合适......
  • 是的 .. 它应该与命名空间有关。我已经把我所有的应用程序都放在了apps 目录中..
  • 我重组了整个项目并将我的应用程序从apps 目录中删除...问题仍然存在:/

标签: django django-templates django-urls


【解决方案1】:

在 Django 1.4.x 中,您不需要任何类型的引号来按名称引用 url ...

{% url index %}

... 应该可以。如果不是,则可能是解析路径时出错。

【讨论】:

    【解决方案2】:

    我很确定您需要在 index 前面加上您的应用名称,例如:{% url 'myapp:index' %}

    https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url

    【讨论】:

    • 这是有道理的。我这样做了,但仍然得到那个错误。也许发生了一些命名空间冲突?我已将我所有的应用程序放在 apps 目录中,并将它们包含在 INSTALLED_APPS...
    猜你喜欢
    • 2012-11-08
    • 2012-09-10
    • 2014-07-29
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多