Opened 7 years ago

Last modified 8 months ago

#10941 new New feature

Add a templatetag to generate querystrings

Reported by: Ben Spaulding Owned by:
Component: Template system Version: master
Severity: Normal Keywords: pagination
Cc: dan.fairs@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Working with pagination and query strings within a template can be painful. Personally, I have never had a situation when I was paginating using a GET parameter where there were not other parameters that needed to be preserved through out the various pages.

Take search, for example. There may be parameters for searching within one or more models, for a particular author and sorting by date. Maintaining all of these parameters within the pagination links takes some serious template logic.

{# Linebreaks added for readability. In real life this would need to be one, long line. #}
<a href="?{% for key, values in request.GET.iterlists %}
  {% ifnotequal key "page" %}
    {% for value in values %}
      {{ key }}={{ value }}&amp;
    {% endfor %}
  {% endifnotequal %}
{% endfor %}page={{ page.next_page_number }}">Next page</a>

That kind of logic shouldn’t be in a template. I have created a patch that would allow for something much simpler, like so:

<a href="?{{ page.next_page_querystring }}">Next page</a>

Though there has been much talk of creating template tags which would produce all-out pagination bars, I believe this particular functionality should be an actual method on the page object for two reasons:

  1. This is basic functionality whose end result is hard to dispute (as opposed to a full pagination bar where markup and features could be disputed eternally),
  2. This does not require the request context processor to be installed.

Note that this patch includes documentation. Tests are still needed. I am not married to the exact implementation, but I and others I have discussed this and feel that this simplicity and fuctionality belong in Django’s pagination.

Attachments (1)

querystring-methods.diff (3.8 KB) - added by Ben Spaulding 7 years ago.
Methods for handling pagination query strings, documentation.

Download all attachments as: .zip

Change History (27)

Changed 7 years ago by Ben Spaulding

Attachment: querystring-methods.diff added

Methods for handling pagination query strings, documentation.

comment:1 Changed 7 years ago by Greg Brown

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

I'm in favour of this proposal

comment:2 Changed 7 years ago by daybreaker

comment:3 Changed 7 years ago by Chris Beaven

Needs tests: set
Triage Stage: UnreviewedDesign decision needed

Patch seems like a good idea - should be brought up in the django-dev google group.

comment:4 in reply to:  3 Changed 7 years ago by Ben Spaulding

Replying to SmileyChris:

Patch seems like a good idea - should be brought up in the django-dev google group.

Thanks for the advice. I have started a discussion on django-dev: Add querystring helper methods to Page class.

comment:5 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: New feature

comment:6 Changed 5 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:7 Changed 5 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

comment:8 Changed 4 years ago by Dan Fairs

Cc: dan.fairs@… added

comment:9 Changed 4 years ago by Florian Apolloner

Triage Stage: Design decision neededAccepted

Oh yeah, I run into this all the time. I think that we should allow customization of the querystring name, eg 'p' instead of 'page'. To that end a simple filter and/or tag might be better.

comment:10 in reply to:  9 Changed 4 years ago by Ben Spaulding

Replying to apollo13:

Oh yeah, I run into this all the time. I think that we should allow customization of the querystring name, eg 'p' instead of 'page'. To that end a simple filter and/or tag might be better.

Just to be clear, drop the helper methods and go with a filter or tag, requiring use of the django.core.context_processors.request? (Sounds fine to me, I just want to be clear on the recommended route so this doesn’t bog down in implementation.) What template tag library would that live in? There doesn’t seem to be a current contrib app it would fit in, and I am not sure it belongs in built-ins if it depends on a context processor that is not installed by default.

comment:11 Changed 17 months ago by Florian Demmer

i really miss this functionality and like the proposed solution. what is missing from the patch to get his merged?

comment:12 Changed 17 months ago by Tim Graham

As indicated by the flags on this ticket "Needs tests".

comment:13 Changed 17 months ago by Florian Demmer

Cc: fdemmer@… added

sorry, i should have seen that. there is too much nice green in the theme ;)

i have created a pr based on the old patch, but slightly changed the implementation, updated MultipleObjectMixin to use the new feature, added a test, updated two existing tests and the documentation.

rfc: https://github.com/django/django/pull/4581

comment:14 Changed 17 months ago by Tim Graham

Needs tests: unset

comment:15 Changed 16 months ago by Tim Graham

Patch needs improvement: set

comment:16 Changed 15 months ago by Ben Spaulding

Owner: changed from nobody to Ben Spaulding
Status: newassigned

comment:17 Changed 15 months ago by Ben Spaulding

Owner: Ben Spaulding deleted
Status: assignednew

comment:18 Changed 15 months ago by Florian Demmer

Patch needs improvement: unset

fixed documentation and added another test; please review updated pr...

comment:19 Changed 14 months ago by Tim Graham

After thinking about this some more, I wonder if something like https://djangosnippets.org/snippets/2428/ would be a more flexible, generic solution (allowing query strings to be generated in other areas besides pagination, for example). I'm not in love with adding request details to the paginator (seems like a poor separation of concerns).

comment:20 Changed 14 months ago by Florian Demmer

Cc: fdemmer@… removed

Two years ago apollo13 set this approach from "design decision needed" to "accepted", then Ben and I worked on getting the old patch up to date and came back fixing all kinds of details and now you think the alternative approach with a template tag is better.

This is your right of course, but please make a final decision and close this issue regarding "Add querystring helper methods to Page class" as wontfix if you decide in favour of a template tag solution, so that nobody else's time is wasted.

I am out.

Last edited 14 months ago by Florian Demmer (previous) (diff)

comment:21 Changed 14 months ago by Tim Graham

We got rid of the "design decision needed" triage stage, so many "accepted" tickets need discussion about implementation details. Often it's difficult to make such decisions without seeing an actual patch. I hoped to start a discussion about the best approach with that comment, not offend you. Of course, I am not trying to waste anyone's time; just trying to craft the best possible APIs for Django.

comment:22 Changed 14 months ago by Tim Graham

From apollo13 on IRC: "for me a tag ala {% query_string page=3 %} which would take the existing qs and rewrite it would be perfect".

comment:23 Changed 14 months ago by Tim Graham

Patch needs improvement: set

comment:24 Changed 13 months ago by Tim Graham

Component: Core (Other)Template system
Has patch: unset
Patch needs improvement: unset
Summary: Add querystring helper methods to Page classAdd a templatetag to generate querystrings

comment:25 Changed 11 months ago by Luke Plant

I think this whole area is solved by django-spurl - https://github.com/j4mie/django-spurl

It builds on urlobject which can be used for Python code: https://github.com/zacharyvoase/urlobject/

There is also furl for Python code: https://github.com/gruns/furl

I don't think we need to duplicate any of these within Django. Having a template tag to do 1/10 of what spurl does is just an invitation to include more and more of spurl's functionality. It might be useful to have some links to spurl in the documentation.

comment:26 Changed 8 months ago by Curtis Maloney

Personally I think it wouldn't hurt to have a helper for the specific case of altering the page number _only_.

In a prior project we ended up needing 4 different querystring manipulators [and I'm sure there could have been more].

One would output the original, less any listed keys, plus any specified keys _overridden_ in value.
One would just add new values.
One would return the original with only the listed keys.
A final just updated the page number.

This still doesn't cover, for instance, when you want to add a second value to an existing key... or.... well, given it's a multi-dict, the options are endless.

So, to reiterate: A _simple_ helper for helping with pagination, which is a problem almost everyone encounters, would be good.

A general purpose querystring manipulator? Leave that to 3rd parties.

Note: See TracTickets for help on using tickets.
Back to Top