Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #10941 -- Added {% query_string %} template tag. #17368

Merged
merged 1 commit into from Oct 26, 2023

Conversation

knyghty
Copy link
Member

@knyghty knyghty commented Oct 15, 2023

https://code.djangoproject.com/ticket/10941
https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521/4

I was unsure about query_string vs querystring as most template tags don't have underscores, but I do prefer with the underscore, personally.

We could also simplify the logic a bit if we say we only allow lists (or nothing at all) when setting something to a list, but it seems nicer to allow any iterable to work.

Credit to Carsten Fuchs for the initial implementation.

@knyghty knyghty force-pushed the query-string branch 3 times, most recently from 4906870 to b69f44b Compare October 15, 2023 21:30
Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knyghty Thanks for this patch 👍 I left initial comments.

django/template/defaulttags.py Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Show resolved Hide resolved
Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this - it'll be helpful to have a nice clean way to manipulate query strings!

docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Outdated Show resolved Hide resolved
django/template/defaulttags.py Outdated Show resolved Hide resolved
Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - overlooked a few things in the docstring.

django/template/defaulttags.py Outdated Show resolved Hide resolved
django/template/defaulttags.py Outdated Show resolved Hide resolved
django/template/defaulttags.py Outdated Show resolved Hide resolved
Copy link
Member

@ngnpope ngnpope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if this iterative reviewing is unhelpful and you'd like me to hold off for a bit - I realise we're practically pairing at this point... 🍐

django/template/defaulttags.py Outdated Show resolved Hide resolved
docs/ref/templates/builtins.txt Show resolved Hide resolved
@felixxm felixxm self-assigned this Oct 26, 2023
@felixxm felixxm changed the title Fixed #10941 -- Added a template tag to generate query strings. Fixed #10941 -- Added {% query_string %} template tag. Oct 26, 2023
Copy link
Member

@felixxm felixxm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@knyghty Thanks for updates 👍

I pushed small edits to tests and docs.

@@ -10,6 +10,7 @@
from django.conf import settings
from django.utils import timezone
from django.utils.html import conditional_escape, escape, format_html
from django.utils.itercompat import is_iterable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious as to why we still use this over isinstance(…, collections.abc.Iterable)? 🤔

Looking at Python's documentation -- see 3) in the first section -- it seems as though it should work perfectly fine. Is there something I'm missing?

It looks as though this was introduced in ad077cc for compatibility with Jython which we no longer support… Perhaps it's time to deprecate is_iterable()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_iterable would presumably be True for objects using the old __getitem__ method, which I don't think is equivalent for isinstance(..., Iterable):

Checking isinstance(obj, Iterable) detects classes that are registered as Iterable or that have an __iter__() method, but it does not detect classes that iterate with the __getitem__() method. The only reliable way to determine whether an object is iterable is to call iter(obj).

That may or may not matter these days (or my reading my be wrong 🤷)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks as though this was introduced in ad077cc for compatibility with Jython which we no longer support… Perhaps it's time to deprecate is_iterable()?

Maybe it's time, but we should discuss it and do it separately. I've changed to is_iterable() to keep the code consistent for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened ticket-34983 for this.

@felixxm felixxm merged commit e67d358 into django:main Oct 26, 2023
36 checks passed
@claudep
Copy link
Member

claudep commented Oct 26, 2023

That's a great one, thanks a ton! ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants