django 模板中的 url 模板标签 [英] url template tag in django template

查看:36
本文介绍了django 模板中的 url 模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我这样定义了我的 urls.py

I defined my urls.py like this

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 模式

I defined the url pattern in my view like this

{% url 'show'%}

然后我收到此错误消息

渲染时发现异常:用参数反转 ''show'''()' 和关键字参数 '{}' 不是找到了.

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

原始回溯(最近调用最后):文件"/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 行,在渲染中nodelist.append(node.render(context))文件"/Library/Python/2.5/site-packages/django/template/defaulttags.py",第 382 行,在渲染中raise e NoReverseMatch: 使用参数 '()' 和 ''show'' 反转未找到关键字参数{}".

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.

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

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

推荐答案

重要提示:这适用于 django 1.4.在 Django 1.5 中,情况正好相反.

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

try using url names without quotes

{% url show %}

不是这个

{% url 'show'%}

这篇关于django 模板中的 url 模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆