#10941 closed New feature (fixed)
Add a templatetag to generate querystrings
Reported by: | Ben Spaulding | Owned by: | Tom Carrick |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | pagination |
Cc: | dan.fairs@…, Ning, Thomas Capricelli, Carsten Fuchs | Triage Stage: | Ready for checkin |
Has patch: | yes | 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 }}& {% 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:
- 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),
- 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)
Change History (50)
by , 16 years ago
Attachment: | querystring-methods.diff added |
---|
follow-up: 4 comment:3 by , 15 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
Patch seems like a good idea - should be brought up in the django-dev google group.
comment:4 by , 15 years ago
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 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:8 by , 12 years ago
Cc: | added |
---|
follow-up: 10 comment:9 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
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 by , 12 years ago
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 by , 10 years ago
i really miss this functionality and like the proposed solution. what is missing from the patch to get his merged?
comment:13 by , 10 years ago
Cc: | 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.
comment:14 by , 10 years ago
Needs tests: | unset |
---|
comment:15 by , 9 years ago
Patch needs improvement: | set |
---|
comment:16 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:17 by , 9 years ago
Owner: | removed |
---|---|
Status: | assigned → new |
comment:18 by , 9 years ago
Patch needs improvement: | unset |
---|
fixed documentation and added another test; please review updated pr...
comment:19 by , 9 years ago
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 by , 9 years ago
Cc: | 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.
comment:21 by , 9 years ago
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 by , 9 years ago
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 by , 9 years ago
Patch needs improvement: | set |
---|
comment:24 by , 9 years ago
Component: | Core (Other) → Template system |
---|---|
Has patch: | unset |
Patch needs improvement: | unset |
Summary: | Add querystring helper methods to Page class → Add a templatetag to generate querystrings |
comment:25 by , 9 years ago
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 by , 9 years ago
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.
comment:27 by , 7 years ago
It's been some time since something happened on this one. i have just stumpled into needing to change query strings in templates, so I thought I'd add my findings.
I have created two rather simple filter which allows you ro edit the query string. django-spurl
already does some of this, however not very elegantly. I think the following three filters would be a great addition to Django that will get you "there" much of the time:
https://pastebin.com/vJ3feNvY
Usage is e.g. <a href="?{% set_query_values page=page.next_page_number %}">»</a>
or <a href="?{% append_query_values selected=item.id %}">Select {{ item.name }}</a>
Note: They all support iterables as values.
I think the only ones probably missing is one for removing a key and one for removing a specific value from a key.
I'm also thinking of merging the set
and append
(also adding remove
) by prepending each key with the action and letting None
clear the list when using set_
. I imagine something like {% modify_query set_level=7 set_nextlevel=None append_consumedlevels=processed_levels_list remove_availablelevels=7 %}
.
What do you think? Any thoughts?
comment:28 by , 7 years ago
I made an implementation of the above mentioned modify_query
.
https://pastebin.com/nGmVdeks
Thoughts and comments are most welcome.
comment:29 by , 7 years ago
Cc: | added |
---|
comment:30 by , 7 years ago
Cc: | added |
---|
comment:31 by , 5 years ago
Is this ticket still valid?
Would a PR with a templatetag be accepted?
If so, I could be working on this.
If anyone can confirm I would appreciate.
Thanks
comment:32 by , 5 years ago
Sure, it's still valid. Read attentively the existing comments.
Maybe the greatest challenge of this ticket is to find a compromise between functionality and simplicity, addressing the more common use cases while accepting it won't address all possible needs.
comment:33 by , 3 years ago
Cc: | added |
---|
I too had reinvented this wheel a couple of years ago, unaware of this ticket. Other implementations above are more powerful, but this one is short:
@register.simple_tag def query_string(*args, **kwargs): """ Combines dictionaries of query parameters and individual query parameters and builds an encoded URL query string from the result. """ query_dict = QueryDict(mutable=True) for a in args: query_dict.update(a) remove_keys = [] for k, v in kwargs.items(): if v is None: remove_keys.append(k) elif isinstance(v, list): query_dict.setlist(k, v) else: query_dict[k] = v for k in remove_keys: if k in query_dict: del query_dict[k] qs = query_dict.urlencode() if not qs: return "" return "?" + qs
It cannot do everything, but covers all my use cases: Keep most of the existing source parameters, but delete, add or change some. Examples:
{# Just repeat the parameters: #} {% query_string request.GET %} {# Add a parameter: #} {% query_string request.GET format='pdf' %} {# Change a parameter: #} {% query_string request.GET page=next_page_number %} {# Overwrite month and year with precomputed values, e.g. with next_month_year = {'month': 1, 'year': 2022}, and clear the day: #} {% query_string request.GET next_month_year day=None %}
I'm aware that posting yet another implementation does not achieve true progress and that the more elaborate implementations above may be more appropriate for inclusion in Django.
Still, I wanted to mention this because in my opinion it keeps the balance between features and simplicity.
comment:34 by , 18 months ago
I would really love to have this: I could work on this, if it is decided how this should work
comment:35 by , 13 months ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:37 by , 13 months ago
Patch needs improvement: | set |
---|
comment:38 by , 13 months ago
Patch needs improvement: | unset |
---|
comment:39 by , 13 months ago
Patch needs improvement: | set |
---|
comment:40 by , 13 months ago
Patch needs improvement: | unset |
---|
comment:41 by , 13 months ago
Triage Stage: | Accepted → Ready for checkin |
---|
comment:49 by , 3 months ago
Further tests to this template tag: https://github.com/django/django/pull/18487
Methods for handling pagination query strings, documentation.