【问题标题】:what's the new syntax of url redirection in a label标签中 url 重定向的新语法是什么
【发布时间】:2014-07-06 20:04:40
【问题描述】:

我的 django 版本是 1.6.3

我从一个项目开始学习 django,该项目有这样的部分

    #blog_list.html
    <h3><a href="{% url detailblog blog.id %}">{{ blog.caption }}</a></h3>
    #urls.py
    url(r'^blog/(?P<id>\d+)/$', 'blog_show', name='detailblog'),

该过程是当我单击 blog_list 显示给我的列表中的一项时,它将通过 a 标签将我带到 blog_show。

但我收到错误消息:

'url' requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.

我不知道这个答案在文档中的位置。

所以请帮帮我,告诉我这个问题的解决方案以及检查文档的正确方法,如果我遇到这种情况或更糟的情况。谢谢!

【问题讨论】:

  • 我找到了解决方案。但是另一个错误出现了异常值:'str' object is not callable

标签: django syntax


【解决方案1】:

您需要将您的网址名称放在引号中:

<h3><a href="{% url 'detailblog' blog.id %}">{{ blog.caption }}</a></h3>

类似的答案here

文档here 中的示例。请注意,在示例中,第一个参数是如何用引号引起来的。

【讨论】:

  • 'str' object is not callable 。为什么会出现这种情况?是否应该对 urls.py 进行任何更改?
  • 您可能缺少视图的完整路径。您在 urls.py 中有哪些用于 blog_show 的导入?见这里:stackoverflow.com/questions/4668040/…
  • 是的。我刚刚想通了。谢谢
猜你喜欢
  • 1970-01-01
  • 2011-10-12
  • 2015-04-02
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 1970-01-01
  • 2021-03-07
  • 2013-11-12
相关资源
最近更新 更多