It is kinda tough to describe what the problem really is. But, do you know when you are creating an interface where
you provide pagination, filters and ordering, and you are keeping the state via URL Get parameters?
For instance if you have different options for ordering, you might think of something like that:
Basically you would be sending the user to the very same page, but passing a GET parameter named order, where
you could do something like that:
PS: This is a minimalist example, if you pass an invalid parameter directly in the querystring you will make the
queryset break. I will avoid adding extra validations so we can focus on the objective of this article.
So far so good. But the problem starts to appear when you add a new control, also via GET parameter. Lets say a
pagination:
What would happen here: if you are ordering the results by the Date and then you move to the next page, you will
lose the ordering preference.
The easiest solution would be something like that:
And then:
The bigger the number of parameters, the bigger the mess in the template.
The Solution
Last week while working on a project I faced this problem again, and I put some time to think of a better/reusable
solution.
So, I came up with this template tag, and I thought about sharing with you guys. Maybe it can be useful for you as
well. Basically you will need the django.template.context_processors.request in your project’s context_processors.
templatetags/templatehelpers.py
And then you use it this way:
Now the template tag will keep that current state of your filters/ordering/pages.