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

查看:148
本文介绍了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:


值得注意的一个不推荐的功能是转向new-styleurl
标签。在Django 1.3之前,像{%url myview%}这样的语法被解释为
不正确(Django将myview视为
视图的文字名称,而不是名为myview的模板变量)。 Django 1.3及以上
引入了{%加载url从未来%}语法引入
更正的行为,其中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
future%}的{%load url),则需要将{%url
myview%}之类的代码更改为{% urlmyview%}。如果您使用{$ load $ url from
future%},可以在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'的错误。 (
我做错了什么?
Thx!

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'

它将通过所有的模板文件并替换为: p>

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

{% url something.else foo bar %}

与此

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

小心,我有点懒惰,可能会打破一些构造,它仍然会尽管如此,您可以轻松找到差异中的错误,而不是手动执行错误。

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天全站免登陆