Back
Feb 28, 2010

Composing multiple views in Django

Vladimir Sidorenko
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:

from django.views.generic.list_detail import object_list

    def filtered_list(request, filterset_class, queryset, *args, **kwargs):
        filterset = filterset_class(request.GET)
        queryset = filterset.filter(queryset)
        kwargs['extra_context']['filterset'] = filterset

        return object_list(request, queryset, *args, **kwargs)

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:

def filtered(view):
        def wrapped(request, filterset_class, queryset, *args, **kwargs):

            filterset = filterset_class(request.GET)
            queryset = filterset.filter(queryset)
            kwargs['extra_context']['filterset'] = filterset

            return view(request, queryset, *args, **kwargs)
        return wrapped

Result

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

@filtered
    @paginated
    def technic_list(*args, **kwargs):
        # ...

Or even like this, in lisp style:

url(r'^technic/$', filtered(sorted(paginated(object_list))), {
        'filterset_class': FilterSet,
        # ...
    }, name='technic_list'),

More thoughts

Oct 11, 2010Technology
Char search in Emacs as in Vim

In VIM there is a command for char search: f. After first use it can be repeated with ;. I like to navigate in line with it. You see that you need to go to bracket in a middle of a line - you press f( and one-two ; and you are there. There's no such command in Emacs, so I had to write my own. I've managed even to implement repetition with ;.

Vladimir Sidorenko
Vladimir Sidorenko
May 22, 2017Technology
Web Application Security: 10 Best Practices

Protection of WEB App is of paramount importance and it should be afforded the same level of security as the intellectual rights or private property. I'm going to cover how to protect your web app.

Rostyslav Stekh
Rostyslav Stekh
May 12, 2010Technology
Twitter API, OAuth and decorators

In my current project I had a task to use twitter API. Twitter uses OAuth for authentication, which is pretty dreary. To avoid fiddling with it all the time, I've moved authentication to decorator. If key is available - nothing happens, just view is launched as usual. It's convenient that there's no need for additional twitter settings in user profile. Code is in article.

Vladimir Sidorenko
Vladimir Sidorenko
Jun 14, 2017Technology
How to Deploy a Django Application on Heroku?

In this article I'll show you how to deploy Django with Celery and Postgres to Heroku.

Vladimir Kalyuzhny
Vladimir Kalyuzhny