django模板中的url模板标签
我试图在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
重要提示:这是针对 django 1.4 的。在 django 1.5 中情况正好相反。
尝试使用不带引号的 url 名称,
而不是 this
IMPORTANT: this was for django 1.4. At django 1.5 it is just the opposite.
try using url names without quotes
not this
问题是你的“show”周围的单引号。将其更改为“显示”,它应该适合您。
请参阅此处
The problem is your single quotes around 'show'. Change this to "show" and it should work out for you.
See here
您可能还有一些观点尚未落实。当使用 {% url ... %} 过滤器时,模板引擎似乎尝试从 urls.py 中的模式查找所有视图。
它通常会显示 urls.py 中最后一个模式的错误。
尝试注释掉您尚未实现的每个 url 模式。
还要确保使用完整路径:
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:
The url template filter looks really unstable. Try to keep future compatibility.
您可能需要更具体地说明您要尝试使用的视图:
You may need to be a little more specific on which view you're trying to use:
无论如何,我遇到了同样的问题,虽然我现在不记得原因,但这为我解决了问题。我正在开发的 SCRUM 应用程序的示例。
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.
Django 1.5 或更高版本:
Django 1.4 或更低版本:
*您可以查看 Django 1.5 发行说明< /a>.
Django 1.5 or above:
Django 1.4 or below:
*You can see Django 1.5 release notes.