{"id":289,"date":"2008-07-05T10:07:00","date_gmt":"2008-07-05T10:07:00","guid":{"rendered":"http:\/\/new.cottee.org\/2008\/07\/django-named-urls-gotcha\/"},"modified":"2008-07-05T10:07:00","modified_gmt":"2008-07-05T10:07:00","slug":"django-named-urls-gotcha","status":"publish","type":"post","link":"https:\/\/cottee.org\/?p=289","title":{"rendered":">Django &#8211; Named URLS Gotcha"},"content":{"rendered":"<p>>A post in two parts. <\/p>\n<p>First of all &#8211; thanks to Magus on #django for pointing out one of my errors to me. I&#8217;ve been working through <a href=\"http:\/\/www.apress.com\/book\/view\/9781590599969\">Practical Django Projects<\/a> over the last couple of days and it talks about having named urls. Take a look at these three one liners:<\/p>\n<pre><br \/># from urls.links.py <br \/><br \/>    (r'^$', 'archive_index',link_info_dict, 'coltrane_link_archive_index'), <br \/><br \/># from urls.py<br \/><br \/>    (r'^weblog\/links\/', include('coltrane.urls.links')),<br \/><br \/># In my template I get the url using <br \/><br \/>  {% url coltrane_link_archive_index  %}<br \/><br \/><\/pre>\n<p>We give our view a name (coltrane_link_archive_index), then we call that view with urls.py so it&#8217;s context will be \/weblog\/links &#8211; 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 &#8211; and of course it saves a lot of tedious typing. Great. <\/p>\n<p>So working through the book, the blog application requires a lot of templates and views and stuff. So I thought I&#8217;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. <\/p>\n<p>Written like this, the problem is obvious, a named view won&#8217;t work if the view it&#8217;s pointing to doesn&#8217;t work. So if your url tag&#8217;s don&#8217;t function &#8211; make sure what they should be taking you to are ok. <\/p>\n<p>But there&#8217;s more. Because when I wrote the underlying views it still wasn&#8217;t working. And I discovered something interesting. I had a problem last night after restarting the web server &#8211; The first time I&#8217;d go to a page I&#8217;d get <\/p>\n<pre><br \/>ImproperlyConfigured: Error while importing URLconf 'coltrane.urls.tags': name 'Link' is not defined<br \/><\/pre>\n<p>For some reason I couldn&#8217;t work out why that was happening last night. I have no idea why I could not &#8211; it&#8217;s blatantly obvious. I wasn&#8217;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&#8217;t written that, but the rest of the site <strong>seemed<\/strong> to be working. I put it down to some strange &#8216;glitch&#8217;. It wasn&#8217;t. <\/p>\n<p>I&#8217;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&#8217;ll have some urls working (up to where it failed) and some not. The second time you hit the site that doesn&#8217;t happen (it thinks the regular expression compilation has completed) and doesn&#8217;t raise the error again. But of course all the urls that failed won&#8217;t be working (hence why url tag wasn&#8217;t working). <\/p>\n<p>On a final note, you&#8217;d think by now that I would have learnt that when you have a &#8216;strange glitch&#8217; there&#8217;s something bigger lurking there that you really need to understand. <\/p>\n<div class=\"blogger-post-footer\"><img width='1' height='1' src='https:\/\/blogger.googleusercontent.com\/tracker\/9210244490645736884-5086000400979780168?l=blog.cottee.org' alt='' \/><\/div>\n","protected":false},"excerpt":{"rendered":"<p>>A post in two parts. First of all &#8211; thanks to Magus on #django for pointing out one of my errors to me. I&#8217;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&#8217;^$&#8217;, &#8216;archive_index&#8217;,link_info_dict, &#8216;coltrane_link_archive_index&#8217;), # from urls.py (r&#8217;^weblog\/links\/&#8217;, include(&#8216;coltrane.urls.links&#8217;)),# In my template I get the url using {% url coltrane_link_archive_index %} We give our view a name&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/cottee.org\/?p=289\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[16,17],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-oldsite","tag-development","tag-django"],"_links":{"self":[{"href":"https:\/\/cottee.org\/index.php?rest_route=\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cottee.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cottee.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cottee.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cottee.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":0,"href":"https:\/\/cottee.org\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"wp:attachment":[{"href":"https:\/\/cottee.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cottee.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cottee.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}