将url标签迁移到django 1.5 [英] Migrate url tags to django 1.5
问题描述
我正在尝试将旧的django应用程序迁移到django 1.5,不同的html文件中有745个URL,如下所示:
I'm trying to migrate an old django application to django 1.5, There are 745 urls in different html files as this way:
{% url url_name %}
如果我没有错,这是不推荐的,可以不再使用django 1.5(如这里),我必须将它们全部转换成:
If I'm not wrong, this was deprecated and can't be used anymore from django 1.5 (as said here), and I have to transform all of them into:
{% url 'url_name' %}
任何想法,不要疯了吗?也许,某种脚本,我不知道...
我无法想象一种方式来替换路径。
Any idea to do this without going crazy? Maybe, some kind of script, I dont know... I can't imagine a way to do it with replace in path.
我是可能缺少一些明显的东西。
I'm probably missing something obvious.
推荐答案
注意:此命令是破坏性的。在运行之前,请使用版本控制或备份您的模板目录。
NOTE: This command is destructive. Use version control or backup your templates directory before running it.
您可以使用sed。从您的模板目录(或目录)运行
You can use sed. From your template directory (or directories) run
sed -i -r -e "s#\{% url ([a-zA-Z0-9_.:-]+)#\{% url '\1'#g" *
表达式匹配 {%url [view name]
,所以提供给url模板标签的参数将不受影响/不变。
The expression matches {% url [view name]
, so arguments provided to the url template tag will be unaffected/unchanged.
要递归运行,
find . -type f -print0 | xargs -0 sed -i -r -e "s#\{% url ([a-zA-Z0-9_.:-]+)#\{% url '\1'#g"
这个sed命令假定您的视图名称只包含字母数字,冒号,破折号,句点和下划线 - 没有其他特殊字符。现在支持命名空间的视图。
This sed command assumes your view names only contain alphanumerics, colons, dashes, periods and underscores - no other special characters. Now supports namespaced views.
针对此 Django 1.4网址模板中的标签进行了测试标签Gist
这篇关于将url标签迁移到django 1.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!