Django Views As Classes with Python’s __call__ Magic Method

I have been using Django off-and-on for about 2 years now. I am really liking the direction, but what bugged me (for the longest time) is the re-usability of the view section.

For those unfamiliar with Django views, they are not strictly “MVC” views. If anything, they act more like controllers, and I treat them as such. It should be a piece of cake to reuse code between these, but usually what happened was that I would define a bunch of private view functions and have them referenced by various view functions. It wasn’t exactly the shining pinnacle of my development career.

While reading the code for django-jqchat while working on a project for my client, I rediscovered the python __call__ magic method. This basically permits you to call an instance of an object like a function. With a little effort, I managed to re-organize the code to make it a little more easy to parse. This is not complete and not debugged, but maybe you will find it useful.

Github Gist Link