Django 1.5 版:'url' 需要一个非空的第一个参数.Django 1.5 中的语法更改 [英] Django release 1.5: 'url' requires a non-empty first argument. The syntax changed in Django 1.5

查看:12
本文介绍了Django 1.5 版:'url' 需要一个非空的第一个参数.Django 1.5 中的语法更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started using Django release 1.5 and got a problem with my old code:

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

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

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

一个值得注意的已弃用功能是向新式"网址的转变标签.在 Django 1.3 之前,像 {% url myview %} 这样的语法被解释错误地(Django 认为myview"是一个视图,而不是名为 myview 的模板变量).Django 1.3 及以上引入了 {% load url from future %} 语法来引入纠正了将 myview 视为变量的行为.

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.

这样做的结果是,如果您不使用 {% load url from未来 %} 在您的模板中,您需要更改像 {% urlmyview %} 到 {% url "myview" %}.如果您使用的是 {% load url from未来 %} 您可以简单地在 Django 1.5 下删除该行

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,参数为(",)",关键字参数{}"未找到.:(我究竟做错了什么?谢谢!

Then I tried: <a href="{% url ‘auto.views.view_post’ post.slug %}"><h3>{{post.title}}</h3></a> but got error Reverse for ‘auto.views.view_post’ with arguments ‘(’',)' and keyword arguments ‘{}’ not found. :( What am I doing wrong? Thx!

推荐答案

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

I really hate doing all this junk by hand, so I wrote a sed script to do it for me. Make sure you have a backup first, then run this in your templates directory:

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

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

It'll go through all of your template files and replace this:

{% url something.else foo bar %}

有了这个

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

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

Be careful, I was a little lazy with this, it might break on some constructs. It's still going to be easier looking for errors in a diff than doing it by hand, though.

这篇关于Django 1.5 版:'url' 需要一个非空的第一个参数.Django 1.5 中的语法更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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