Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django release 1.5: 'url' requires a non-empty first argument. The syntax changed in Django 1.5

Tags:

url

django

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> 

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

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

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!

like image 583
Wolter Avatar asked Feb 14 '13 19:02

Wolter


1 Answers

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 %} 

with this

{% 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.

like image 108
Stephen Fuhry Avatar answered Sep 22 '22 15:09

Stephen Fuhry