>Django – Named URLS Gotcha

>Django – Named URLS Gotcha

>A post in two parts.

First of all – thanks to Magus on #django for pointing out one of my errors to me. I’ve been working through Practical Django Projects over the last couple of days and it talks about having named urls. Take a look at these three one liners:


# from urls.links.py

(r'^$', 'archive_index',link_info_dict, 'coltrane_link_archive_index'),

# from urls.py

(r'^weblog/links/', include('coltrane.urls.links')),

# In my template I get the url using

{% url coltrane_link_archive_index %}

We give our view a name (coltrane_link_archive_index), then we call that view with urls.py so it’s context will be /weblog/links – and then in our template we can just use the url tag to call that named url. If the url gets moved, your templates will still work – and of course it saves a lot of tedious typing. Great.

So working through the book, the blog application requires a lot of templates and views and stuff. So I thought I’d just do the main one and get the minor ones working later. But I did check to make sure that links to those views were working and they were not.

Written like this, the problem is obvious, a named view won’t work if the view it’s pointing to doesn’t work. So if your url tag’s don’t function – make sure what they should be taking you to are ok.

But there’s more. Because when I wrote the underlying views it still wasn’t working. And I discovered something interesting. I had a problem last night after restarting the web server – The first time I’d go to a page I’d get


ImproperlyConfigured: Error while importing URLconf 'coltrane.urls.tags': name 'Link' is not defined

For some reason I couldn’t work out why that was happening last night. I have no idea why I could not – it’s blatantly obvious. I wasn’t doing an import of the Link model for my tags url file. But I was ignoring it yesterday because I found that if you just asked for the page again it then displays (not the tags page, I hadn’t written that, but the rest of the site seemed to be working. I put it down to some strange ‘glitch’. It wasn’t.

I’m guessing here somewhat but I think what happens is the first time you load your site up, Django precompiles the regular expressions used for your urls. During that compile if it hits an error you’ll have some urls working (up to where it failed) and some not. The second time you hit the site that doesn’t happen (it thinks the regular expression compilation has completed) and doesn’t raise the error again. But of course all the urls that failed won’t be working (hence why url tag wasn’t working).

On a final note, you’d think by now that I would have learnt that when you have a ‘strange glitch’ there’s something bigger lurking there that you really need to understand.

2 thoughts on “>Django – Named URLS Gotcha

  1. Hello blogger, i must say you have high quality
    articles here. Your page should go viral. You need initial traffic
    only. How to get it? Search for; Mertiso’s tips
    go viral

Leave a Reply

Your email address will not be published. Required fields are marked *