Back
Feb 28, 2010

Composing multiple views in Django

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'),

Subscribe for the news and updates

More thoughts
May 9, 2018Technology
How to Generate PDF Files in Python with Xhtml2pdf, WeasyPrint or Unoconv

Programmatic generation of PDF files is a frequent task when developing applications that can export reports, bills, or questionnaires. In this article, we will consider three common tools for creating PDFs, including their installation and converting principles.

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.

Feb 28, 2017Technology
Passing pieces of markup to components in Angular 2 and problems with dynamic content

In this article we'll research how to pass custom markup to Angular components and how to create different types of dynamic components.

Sep 22, 2016Technology
Angular Form Validation

In this article, we will describe some useful scripts and directives we use with angular form validation in our projects.

Mar 4, 2011Technology
Css sprite generation

I've created this small sprite to create css sprites. It glues images from directory directory into single file and generates corresponding css.

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 ;.