django模板中的url模板标签

发布于 2024-08-12 05:38:23 字数 1036 浏览 9 评论 0原文

我试图在 django 中使用 url 模板标记,但不幸运,

我像这样定义了我的 urls.py

urlpatterns = patterns('',
    url(r'^analyse/$',              views.home,  name="home"),
    url(r'^analyse/index.html',     views.index, name="index"),
    url(r'^analyse/setup.html',     views.setup, name="setup"),
    url(r'^analyse/show.html',      views.show,  name="show"),
    url(r'^analyse/generate.html',  views.generate, name="generate"),

我像这样在我的视图中定义了 url 模式

{% url 'show'%}

然后我收到了此错误消息

渲染时捕获异常: 带参数的“show”的反转 '()' 和关键字参数 '{}' 不是 找到了。

原始回溯(最近调用 最后):文件 “/Library/Python/2.5/site-packages/django/template/debug.py”, 第 71 行,在 render_node 中 结果 = node.render(context) 文件 “/Library/Python/2.5/site-packages/django/template/defaulttags.py”, 第 155 行,渲染中 节点列表.append(节点.渲染(上下文)) 文件 “/Library/Python/2.5/site-packages/django/template/defaulttags.py”, 第 382 行,渲染中 raise e NoReverseMatch:使用参数 '()' 反转 ''show'' 和 未找到关键字参数“{}”。

我想知道为什么django渲染失败?在模板中定义它的正确方法是什么?

I was trying to use the url template tag in django, but no lucky,

I defined my urls.py like this

urlpatterns = patterns('',
    url(r'^analyse/

I defined the url pattern in my view like this

{% url 'show'%}

then I got this error message

Caught an exception while rendering:
Reverse for ''show'' with arguments
'()' and keyword arguments '{}' not
found.

Original Traceback (most recent call
last): File
"/Library/Python/2.5/site-packages/django/template/debug.py",
line 71, in render_node
result = node.render(context) File
"/Library/Python/2.5/site-packages/django/template/defaulttags.py",
line 155, in render
nodelist.append(node.render(context))
File
"/Library/Python/2.5/site-packages/django/template/defaulttags.py",
line 382, in render
raise e NoReverseMatch: Reverse for ''show'' with arguments '()' and
keyword arguments '{}' not found.

I am wondering why django failed to render? what is the right way to define it in the tempalte?

, views.home, name="home"), url(r'^analyse/index.html', views.index, name="index"), url(r'^analyse/setup.html', views.setup, name="setup"), url(r'^analyse/show.html', views.show, name="show"), url(r'^analyse/generate.html', views.generate, name="generate"),

I defined the url pattern in my view like this

then I got this error message

Caught an exception while rendering:
Reverse for ''show'' with arguments
'()' and keyword arguments '{}' not
found.

Original Traceback (most recent call
last): File
"/Library/Python/2.5/site-packages/django/template/debug.py",
line 71, in render_node
result = node.render(context) File
"/Library/Python/2.5/site-packages/django/template/defaulttags.py",
line 155, in render
nodelist.append(node.render(context))
File
"/Library/Python/2.5/site-packages/django/template/defaulttags.py",
line 382, in render
raise e NoReverseMatch: Reverse for ''show'' with arguments '()' and
keyword arguments '{}' not found.

I am wondering why django failed to render? what is the right way to define it in the tempalte?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不…忘初心 2024-08-19 05:38:23

重要提示:这是针对 django 1.4 的。在 django 1.5 中情况正好相反。

尝试使用不带引号的 url 名称,

{% url show %}

而不是 this

{% url 'show'%}

IMPORTANT: this was for django 1.4. At django 1.5 it is just the opposite.

try using url names without quotes

{% url show %}

not this

{% url 'show'%}
水波映月 2024-08-19 05:38:23

问题是你的“show”周围的单引号。将其更改为“显示”,它应该适合您。

请参阅此处

The problem is your single quotes around 'show'. Change this to "show" and it should work out for you.

See here

一个人的旅程 2024-08-19 05:38:23

您可能还有一些观点尚未落实。当使用 {% url ... %} 过滤器时,模板引擎似乎尝试从 urls.py 中的模式查找所有视图。

它通常会显示 urls.py 中最后一个模式的错误。

尝试注释掉您尚未实现的每个 url 模式。

还要确保使用完整路径:

{% url myapp.views.home %}

url 模板过滤器看起来确实不稳定。尝试保持未来兼容性

You maybe have some views not implemented yet. It looks like the template engine tries to find all views from the patterns in urls.py when the {% url ... %} filter is used.

It usually shows an error for your last pattern in urls.py.

Try comment out every url pattern you did not implement yet.

Also make sure you use the full path:

{% url myapp.views.home %}

The url template filter looks really unstable. Try to keep future compatibility.

℡Ms空城旧梦 2024-08-19 05:38:23

您可能需要更具体地说明您要尝试使用的视图:

{% url appname.views.show %}

You may need to be a little more specific on which view you're trying to use:

{% url appname.views.show %}
随心而道 2024-08-19 05:38:23

无论如何,我遇到了同样的问题,虽然我现在不记得原因,但这为我解决了问题。我正在开发的 SCRUM 应用程序的示例。

url(r'^
, 'scrum.views.index',  name='scrum-index'),

For what is is worth, I had the same issue and while I do not remember the reason why now, this resolved it for me. Example from a SCRUM app I was working on.

url(r'^
, 'scrum.views.index',  name='scrum-index'),
各空 2024-08-19 05:38:23

Django 1.5 或更高版本:

{% url 'show'%}

Django 1.4 或更低版本:

{% url show %}

*您可以查看 Django 1.5 发行说明< /a>.

Django 1.5 or above:

{% url 'show'%}

Django 1.4 or below:

{% url show %}

*You can see Django 1.5 release notes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文