How to paginate Django with other get variables?

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

<a href="https://stackoverflow.com/questions/2047622/?{% 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/

Leave a Comment