Django in-url paging and reverse, url, permalink, etc

django01a.jpg

Here is a quick note that my coworker George discovered that I went around and changed in some of our development code…

Django’s url structure allows you to do your paging inline (bla.com/view/3/ instead of bla.com/view?page=3) which is better for url based page cache retrieval (at least in rails) as well as search spider optimization… and it is just more visually pleasing for bookmarks and link sharing. However, reverse(), url(), permalink() get confused on complex url rule.

The below rule in urls.py matches bla.com/view, bla.com/view/, bla.com/view/2, and bla.com/2/ :

(r'^view/(?P
[\d]+)?/*$', 'some_view'),

This works great! However, this works great if you are constructing your own links. If you make use of django’s reverse() or similar “view to url” creation tools, then you get some funky issues.

The final solution after lots of reading was to create 2 url rules in urls.py

(r'^view/$', 'some_view'),
(r'^view/(?P
[\d]+)?/$', 'some_view'),

This is somewhat touched on in the django documentation, but according to the docs the solution above, would get “confused” since two address point to the same view. However, in the above case, they have different argument lists. Word.

blog comments powered by Disqus
line
footer
Copyright © 1997 - 2010 Blaine Garrett All Rights Reserved