How to paginate Django with other get variables?

I am having problems using pagination in Django. Take the URL below as an example:

http://127.0.0.1:8000/users/?sort=first_name

On this page I sort a list of users by their first_name. Without a sort GET variable it defaults to sort by id.

Now if I click the next link I expect the following URL:

http://127.0.0.1:8000/users/?sort=first_name&page=2

Instead I lose all get variables and end up with

http://127.0.0.1:8000/users/?page=2

This is a problem because the second page is sorted by id instead of first_name.

If I use request.get_full_path I will eventually end up with an ugly URL:

http://127.0.0.1:8000/users/?sort=first_name&page=2&page=3&page=4

What is the solution? Is there a way to access the GET variables on the template and replace the value for the page?

I am using pagination as described in Django's documentation and my preference is to keep using it. The template code I am using is similar to this:

{% if contacts.has_next %}
    <a href="?page={{ contacts.next_page_number }}">next</a>
{% endif %}

Solution 1:

I thought the custom tags proposed were too complex, this is what I did in the template:

<a href="?{% url_replace request 'page' paginator.next_page_number %}">

And the tag function:

@register.simple_tag
def url_replace(request, field, value):

    dict_ = request.GET.copy()

    dict_[field] = value

    return dict_.urlencode()

If the url_param is not yet in the url, it will be added with value. If it is already there, it will be replaced by the new value. This is a simple solution the suits me, but does not work when the url has multiple parameters with the same name.

You also need the RequestContext request instance to be provided to your template from your view. More info here:

http://lincolnloop.com/blog/2008/may/10/getting-requestcontext-your-templates/

Solution 2:

I think url_replace solution may be rewritten more elegantly as

from urllib.parse import urlencode
from django import template

register = template.Library()

@register.simple_tag(takes_context=True)
def url_replace(context, **kwargs):
    query = context['request'].GET.copy()
    query.update(kwargs)
    return query.urlencode()

with template string simplified to

<a href="?{% url_replace page=paginator.next_page_number %}">

Solution 3:

After some playing around I found a solution... although I don't know if it's really a good one. I'd prefer a more elegant solution.

Anyway I pass the request to the template and am able to access all the GET variables via request.GET. Then I loop through the GET dictionary and as long as the variable isn't page I print it.

{% if contacts.has_previous %}
    <a href="?page={{ contacts.previous_page_number }}{% for key,value in request.GET.items %}{% ifnotequal key 'page' %}&{{ key }}={{ value }}{% endifnotequal %}{% endfor %}">previous</a>
{% endif %}

<span class="current">
    Page {{ contacts.number }} of {{ contacts.paginator.num_pages }}.
</span>

{# I have all of this in one line in my code (like in the previous section), but I'm putting spaces here for readability.  #}
{% if contacts.has_next %}
    <a href="?page={{ contacts.next_page_number }}
        {% for key,value in request.GET.items %}
            {% ifnotequal key 'page' %}
                &{{ key }}={{ value }}
            {% endifnotequal %}
        {% endfor %}
    ">next</a>
{% endif %}

Solution 4:

In your views.py you will somehow access the criteria on which you sort, e.g. first_name. You'll need to pass that value to the template and insert it there to remember it.

Example:

{% if contacts.has_next %}
    <a href="?sort={{ criteria }}&page={{ contacts.next_page_number }}">next</a>
{% endif %}