Django release 1.5: "url" requires a non-empty first argument. Syntax changed in Django 1.5

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

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

Error: "url" requires a non-empty first argument. For the syntax changed in Django 1.5, see the docs. Documents:

One obsolete feature worth noting is the switch to the "new style" tag. Prior to Django 1.3, syntax like {% url myview%} was misinterpreted (Django is considered "myview" as the literal name of view, not a template variable named myview). Django 1.3 and later introduced the syntax {% load url from future%} to introduce corrected behavior when myview was considered as a variable.

This means that if you do not use {% load url from future%} in your templates, you will need to change the tags, for example {% url myview%} - {% url "myview"%}. If you use {% load url from future%} you can simply delete this line under Django 1.5

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

+65
url django
Feb 14 '13 at 19:25
source share
6 answers

I really hate doing all this garbage manually, so I wrote a sed script to do this for me. First make sure you have a backup, then run this in your templates directory:

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

It will look through all your template files and replace this:

 {% url something.else foo bar %} 

with this

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

Be careful, I was a little lazy about this, it may break on some designs. However, it will be easier to look for errors in diff than to do it manually.

+87
Mar 12 '13 at 23:05
source share

Firstly, you correctly used single quotes for the name of the view, i.e. 'auto.views.view_post' .

Now temporarily remove the url tag and make sure that {{ post }} and {{ post.slug }} give you the expected values. The arguments '('',)' error message indicates that the post.slug problem.

+3
Feb 14 '13 at 19:49
source share

To exclude the .git folder and avoid the error, MacOS added blank quotation marks to the -i option. '' Example:

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

But I like this approach (MacOS):

 grep '{% url' -lrZ . | xargs -0 sed -i '' 's/ url \([^" >][^ >]*\)/ url "\1"/g' 
+3
May 7 '13 at 22:19
source share

If you are using Mac OS, you need to pass -e

 find . -type f -print0 | xargs -0 sed -i -e 's/ url \([^" >][^ >]*\)/ url "\1"/g' 
0
Mar 30 '13 at 10:27
source share

I got the “ILLEGAL BYTE OF SEQUENCE” error from sed with most of these recipes, which I could fix by doing this in the first place:

 LANG=C 

However, these recipes generated a lot of false positives in my project, and we already had a combination of URL names that were single, double quotes, or incorrect. It was a mess. The cleanest approach was to simply search the template directory with my editor (Sublime) in regex mode for:

 \{\%\ url\ [^'] \{\%\ url\ [^"] 

(find all instances that have not yet been specified) and view them visually. It turned out to be faster and cleaner than trying to automate it, and then clean up the mess after that.

0
Aug 14 '13 at 22:54
source share

you may also need to regexp-replace "\{% url "([\w:]+)" for "\{% url '$1' so that aviod syntax errors such as <a href="{% url "foo:bar" %}">baz</a>

0
Apr 01 '15 at 14:28
source share



All Articles