- March 7, 2009
- views rfc
- 0 (after 0 ratings)
This snippet is working code, however it is not intended for proper use, rather to garner comment on an alternative style of view - using a class for views, rather than a function.
While working with views, I've often felt that the traditional django code layout splits concerns in an unnatural fashion. The parameters for a view must be maintained in both the urls file as well as the view for example, and there is no neat way of grouping multiple accessor for a REST resource.
This 'shim' code aims to propose an alternative architecture, that is interchangeable with the existing system.
Rather than include a tuple of urls, instead a list of classes is provided. Each class models a resource, or page.
Page objects have a url property and name property, so it is therefor trivial to reconstruct the url tuple from the class, but allow more simplicity and structure in relating the methods of the resource.
You may notice that this structure closely follows the architecture of the web.py framework - this syntax did indeed play a part in the concept for such a structure.
While this paradigm may not be suitable in all situations, I believe it promotes a simpler, more encapsulated views architecture.
Any comments and feedback are welcomed.
Example usage (untested, sorry):
class Homepage(Page): def get(request): return HttpResponse("Hello World")
urlpatterns = patterns('', Homepage, url('^existing$', existing.view.function, name = "foo"), )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
from django.conf.urls.defaults import patterns as django_patterns from django.conf.urls.defaults import url def patterns(*urls): pages =  for cls in urls: if isinstance(cls, type): inst = cls() pages.append(url(inst.url, inst.resource, name = inst.name)) else: #oldstyle resources pages.append(cls) return django_patterns(*pages) class Page(object): url = r"^$" def resource(self, request, *args, **kwargs): if request.method == "GET": return self.get(request, *args, **kwargs) if request.method == "POST": return self.post(request, *args, **kwargs) @property def name(self): return "%s-%s" % (self.__class__.__name__, "page")