gpt4 book ai didi

Django 版本 1.5 : 'url' requires a non-empty first argument. Django 1.5 中的语法发生了变化

转载 作者:行者123 更新时间:2023-11-28 19:33:22 24 4
gpt4 key购买 nike

我开始使用 Django 1.5 版,但我的旧代码出现了问题:

<a href="{% url auto.views.viewpost post.slug %}"><h3>{{post.title}}</h3></a>

错误:“url”需要一个非空的第一个参数。 Django 1.5 中的语法发生了变化,请参阅文档。文档:

One deprecated feature worth noting is the shift to “new-style” url tag. Prior to Django 1.3, syntax like {% url myview %} was interpreted incorrectly (Django considered "myview" to be a literal name of a view, not a template variable named myview). Django 1.3 and above introduced the {% load url from future %} syntax to bring in the corrected behavior where myview was seen as a variable.

The upshot of this is that if you are not using {% load url from future %} in your templates, you’ll need to change tags like {% url myview %} to {% url "myview" %}. If you were using {% load url from future %} you can simply remove that line under Django 1.5

然后我尝试: <a href=“{% url ‘auto.views.view_post’ post.slug %}”><h3>{{post.title}}</h3></a>但在“auto.views.view_post”中出现错误 Reverse with arguments ‘(’’,)’ 和关键字参数 ‘{}’ not found。 :(我究竟做错了什么?谢谢!

最佳答案

我真的很讨厌手工做所有这些垃圾,所以我写了一个 sed 脚本来帮我做这些。确保先备份,然后在模板目录中运行:

find . -type f -print0 | xargs -0 sed -i 's/{% url \([^" >][^ >]*\)/{% url "\1"/g'

它将遍历所有模板文件并替换它:

{% url something.else foo bar %}

有了这个

{% url "something.else" foo bar %}

小心,我有点懒惰,它可能会破坏某些结构。不过,在 diff 中查找错误仍然比手动查找更容易。

关于Django 版本 1.5 : 'url' requires a non-empty first argument. Django 1.5 中的语法发生了变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14882491/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com