Use double quotes for {% url %} on Django

I recently commented about using the right quotes with Python and simplejson and today I found a similar issue with Django.

It happens when using the {% url %} tag on the template system in Django 1.0. {% url %} helps you to avoid hardcoding links and relies on naming your url patterns.

Let's suppose we have a url like this in our URLConf:

url(r'^add/chatroom/(?P\w+)$', 'myapp_chatroom_add', name='myapp_chatroom_add'),

Now you could insert {% url %} in your template to get the url for a link:

{% url myapp_chatroom_add chatroom_data="abc" %}

Notice I'm passing the string "abc" using double quotes as the chatroom_data parameter.

And here's the important part, these two won't work:

{% url myapp_chatroom_add chatroom_data=abc %}

which does not use quotes at all, or this one:

{% url myapp_chatroom_add chatroom_data='abc' %}

which uses single quotes.

The errors may vary depending on how your code works but the fact is to remember that when passing string based parameters to {% url %} you need to always use double quotes.

If you're using numbers you could use either no quotes or double quotes. Single quotes will never work.

Trackback URL for this post:

http://www.ventanazul.com/webzine/trackback/126

Actually it's a common bug:

Actually it's a common bug.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h1> <h2> <h3> <h4>
  • Lines and paragraphs break automatically.

More information about formatting options