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

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
Feb 18, 2010Technology
Absolute urls in models

Everybody knows about permalink, but it's usually used only in get_absolute_url. I prefer to use it for all related model urls.class Event(models.Model):# [email protected] edit_url(self):return ('event_edit', (self.pk, ))And then in template:<a href="{{ event.edit_url }}">Редактировать событие</a>

Vladimir Sidorenko
Vladimir Sidorenko
Mar 02, 2017Technology
API versioning with django rest framework?

We often handling API server updates including backwards-incompatible changes when upgrading web applications. At the same time we update the client part, therefore, we did not experience any particular difficulties.

Igor Tokarev
Igor Tokarev
May 10, 2018Technology
How to Build a Cloud-Based Leads Management System for Universities

Lead management is an important part of the marketing strategy of every company of any size. Besides automating various business processes, privately-held organizations should consider implementing an IT solution that would help them manage their leads. So, how should you make a web-based leads management system for a University in order to significantly increase sales?

Vladimir Sidorenko
Vladimir Sidorenko