Back
Feb 28, 2010

Composing multiple views in Django

Vladimir Sidorenko

In UNIX way, each view should solve single task. This is good idea, but sometimes we need to mix logic of different views on same page. Filter, sort, paginate, or, for example, add comment on product page. In this article I'll show how we can mix such multiple views.

Idea

We can write views as decorators.

Example

Simplified filtration can look like this:

false

This is simple and straightforward, but in order to add some tricky pagination, we have to copy entire view and add more code to it.

Instead, we can create decorator like this:

false

Result

Assuming, that we have similar decorators, sorted и paginated, we can easily combine them:

false

Or even like this, in lisp style:

false
More thoughts