Saturday, July 25, 2009

[geek] Splitting django views

Geekiness ahead; you have been warned.

Steve wrote a post about splitting django views. Consider this as another viewpoint / way of doing the same.

The primary problem is to split django's into multiple different modules, ostensibly to make them smaller and more modular (duh).

To begin with, I'm not entirely certain the problem exists - in MVC or MD paradigms, the view is typically very tiny and almost entirely devoid of logic. It is there only to reflect changes made to the model. Consequently, it is unclear that the needs splitting at all. I have not done enough large scale django development (my projects have been of the tiny homebrew variety), so lets assume that views become unmanageable in the long run and need to be split up.

Lets say you have a setup and have the following directory structure:

> ls*

and looks like this:

> cat

from django.http import HttpResponse

def index(request):
return HttpResponse('index')

def foo(request):
return HttpResponse('foo')

def bar(request):
return HttpResponse('bar')

Now we'd like to break apart views into a directory, so we change it to:
> ls -R
.:* views/


where each file in the views directory has its own little function:

> cat views/

from django.http import HttpResponse

def foo(request):
return HttpResponse('foo')

At this point, note that there is no at all.

The original url resolver obviously will not work because it looks like this:
urlpatterns = patterns('',
(r'^foo/', ''),
(r'^bar/', ''),
(r'^$', 'views.index'),

So we change that too:
urlpatterns = patterns('',
(r'^foo/', ''),
(r'^bar/', ''),
(r'^$', 'views.index.index'),

and the site is back up as normal.

I think this approach - explicit directions in is much cleaner and much more inkeeping with django's spirit of avoiding magic. Note that the other approach which basically imported all functions the minute was touched would maintain the mappings, thus saving a bit of work. However, I think it would confuse the heck out of people, not to mention result in wierd name collisions if any of the individual view files happen to export the same function.


Steve Lacy said...

Yeah, this is actually where I started, but I'm *really* disappointed by Python making me say "" It just feels *so* wrong to me, like there must be some better way, which is where I started saying to myself "there must be a better way".

Anyway, yeah, hacking seems horrible. I think this is what they do for the Google AppEngine version of Django, but I couldn't find a copy of that (although I didn't look very hard).

Leo said...

Why wouldn't you just put an in /views?

'app.views' is just a python package.