URL Mappings in Django Keep getting better

by Jeremy Jones

Django is the web application framework that I'm currently using. It has this really nifty feature of allowing you to create URL mappings which do regex matching on requests coming in and then pass them off to "views" which process the request. You can read more about it here .

Here is a little snippet of a URL conf from one of my current projects:

(r'^stores/$', "dj_store.stores.views.stores"),


Using the view "dj_store.stores.views.stores" caused a naming conflict. I was a little bummed by that because I wanted to keep a clean URL structure and provide a simple URL interface such that a GET of a simple resource name (such as "/stores/") would display a list of all available resources of that type. (So, a GET of "/stores/" should display a list of all stores.) I started thinking of acceptable alternatives for what to change the URL to: "/get_stores/", "/show_stores/", etc.. None of these choices appealed to me because it felt like I was uglying up my URL just to avoid a naming conflict. But then it occurred to me that I didn't have to do that. In Django, the URL is independent of the Python function that handles the request.

So, I changed the above URL mapping to:

(r'^stores/$', "dj_store.stores.views.all_stores"),


(and obviously changed the view to "all_stores") and I was able to keep the URL naming I liked and avoid a Python naming conflict. I know this is really simple and really obvious, but sometimes I can't help being like a kid who is fascinated by playing with a cardboard box. And I think my "duh" moment and fascination with simplicity and obviousness doesn't detract from the excellent design decisions of the Django developers regarding their URL configuration.