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
Apr 11, 2024Technology
Test Analysis at the Feature Level

In the previous article, we learned about test analysis for a product in general, and now we are ready to go further and look at test analysis for specific features.

Mar 18, 2024Technology
From boring to exciting: turn learning to code into an adventure

Tired of boring programming courses where you're forced to read thick textbooks and write code that's never used? Need a platform that makes learning fun and exciting? Then you're in the right place!

Dec 13, 2022Technology
How to create a timelapse video from frames

We’ll tell you how to create a video timelapse from a sequence of snapshots and provide customers with video playlists optimized for browser playback.

Sep 1, 2021TechnologyBusiness
Top 10 Web Development Frameworks in 2021 - 2022

We have reviewed the top web frameworks for server and client-side development and compared their pros and cons. Find out which one can be a great fit for your next project.

Feb 28, 2017Technology
How to write an API in Django

There is such a term as Remote Procedure Call (RPC). In other words, by using this technology, programs can call functions on remote computers. There are many ways to implement RPC.

Jan 22, 2017Technology
Django vs Rails Performance

This article is aimed for beginners, who are trying to choose between Ruby on Rails and Django. Let’s see which is fastest and why.