[Django] #35529: Have the template tag query_string support Mapping[str, Any] as an argument

23 views
Skip to first unread message

Django

unread,
Jun 18, 2024, 3:25:57 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
-------------------------------------------+------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
Based off this discussion: https://forum.djangoproject.com/t/adding-a
-template-tag-to-generate-query-strings/24521/28

Currenty `query_string` only supports a `QueryDict` as an argument.

{{{#!diff
--- a/tests/template_tests/syntax_tests/test_query_string.py
+++ b/tests/template_tests/syntax_tests/test_query_string.py
@@ -93,6 +93,14 @@ class QueryStringTagTests(SimpleTestCase):
)
self.assertEqual(output, "?a=2&b=2")

+ @setup(
+ {"query_string_dict": "{% query_string my_dict %}"}
+ )
+ def test_query_string_with_explicit_query_dict_and_no_request(self):
+ context = {"my_dict": {"a": 1, "b": 2}}
+ output = self.engine.render_to_string("query_string_dict",
context)
+ self.assertEqual(output, "?a=1&b=2")
+
@setup({"query_string_no_request_no_query_dict": "{% query_string
%}"})
def test_query_string_without_request_or_explicit_query_dict(self):
msg = "'Context' object has no attribute 'request'"
}}}


Note: I believe this is backwards compatible so not a release blocker
--
Ticket URL: <https://code.djangoproject.com/ticket/35529>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jun 18, 2024, 3:36:29 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Sarah Boyce:

Old description:

> Based off this discussion: https://forum.djangoproject.com/t/adding-a
> -template-tag-to-generate-query-strings/24521/28
>
> Currenty `query_string` only supports a `QueryDict` as an argument.
>
> {{{#!diff
> --- a/tests/template_tests/syntax_tests/test_query_string.py
> +++ b/tests/template_tests/syntax_tests/test_query_string.py
> @@ -93,6 +93,14 @@ class QueryStringTagTests(SimpleTestCase):
> )
> self.assertEqual(output, "?a=2&amp;b=2")
>
> + @setup(
> + {"query_string_dict": "{% query_string my_dict %}"}
> + )
> + def test_query_string_with_explicit_query_dict_and_no_request(self):
> + context = {"my_dict": {"a": 1, "b": 2}}
> + output = self.engine.render_to_string("query_string_dict",
> context)
> + self.assertEqual(output, "?a=1&amp;b=2")
> +
> @setup({"query_string_no_request_no_query_dict": "{% query_string
> %}"})
> def test_query_string_without_request_or_explicit_query_dict(self):
> msg = "'Context' object has no attribute 'request'"
> }}}
>

> Note: I believe this is backwards compatible so not a release blocker

New description:

Based off this discussion: https://forum.djangoproject.com/t/adding-a
-template-tag-to-generate-query-strings/24521/28

Currenty `query_string` only supports a `QueryDict` as an argument,
consensus appears to want to update this to also support `Mapping[str,
Any]`

{{{#!diff
--- a/tests/template_tests/syntax_tests/test_query_string.py
+++ b/tests/template_tests/syntax_tests/test_query_string.py
@@ -93,6 +93,14 @@ class QueryStringTagTests(SimpleTestCase):
)
self.assertEqual(output, "?a=2&amp;b=2")

+ @setup(
+ {"query_string_dict": "{% query_string my_dict %}"}
+ )
+ def test_query_string_with_explicit_query_dict_and_no_request(self):
+ context = {"my_dict": {"a": 1, "b": 2}}
+ output = self.engine.render_to_string("query_string_dict",
context)
+ self.assertEqual(output, "?a=1&amp;b=2")
+
@setup({"query_string_no_request_no_query_dict": "{% query_string
%}"})
def test_query_string_without_request_or_explicit_query_dict(self):
msg = "'Context' object has no attribute 'request'"
}}}


Note: I believe this is backwards compatible so not a release blocker

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:1>

Django

unread,
Jun 18, 2024, 3:41:08 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Description changed by Sarah Boyce:

Old description:

> Based off this discussion: https://forum.djangoproject.com/t/adding-a
> -template-tag-to-generate-query-strings/24521/28
>
+ def test_query_string_with_explicit_dict_and_no_request(self):
+ context = {"my_dict": {"a": 1, "b": 2}}
+ output = self.engine.render_to_string("query_string_dict",
context)
+ self.assertEqual(output, "?a=1&amp;b=2")
+
@setup({"query_string_no_request_no_query_dict": "{% query_string
%}"})
def test_query_string_without_request_or_explicit_query_dict(self):
msg = "'Context' object has no attribute 'request'"
}}}


Note: I believe this is backwards compatible so not a release blocker

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:2>

Django

unread,
Jun 18, 2024, 3:48:55 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Carsten Fuchs):

Isn't this how it is already implemented?

My understanding of the discussion was that
[https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-
query-strings/24521/25 more than one] dict could be supported.
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:3>

Django

unread,
Jun 18, 2024, 6:31:06 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Sarah Boyce):

Replying to [comment:3 Carsten Fuchs]:
> Isn't this how it is already implemented?

No I don't think so. It must be a `QueryDict`. The attached test currently
fails with

{{{
...
File "pathtodjango\django\template\defaulttags.py", line 1208, in
query_string
query_string = query_dict.urlencode()
^^^^^^^^^^^^^^^^^^^^
AttributeError: 'dict' object has no attribute 'urlencode'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:4>

Django

unread,
Jun 18, 2024, 9:05:28 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Carsten Fuchs):

> No I don't think so. It must be a `QueryDict`.

Oh, right. I'm sorry.
These are two separate issues then: Supporting `Mapping[str, Any]` and
supporting more than one of them.
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:5>

Django

unread,
Jun 18, 2024, 11:15:28 AM6/18/24
to django-...@googlegroups.com
#35529: Have the template tag query_string support Mapping[str, Any] as an argument
---------------------------------+--------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Sarah Boyce):

Replying to [comment:5 Carsten Fuchs]:
> Oh, right. I'm sorry.
> These are two separate issues then: Supporting `Mapping[str, Any]` and
supporting more than one of them.

Don't say sorry, they are very related as issues and go well together,
thank you for pointing it out. Updated the ticket to capture both of these
👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35529#comment:6>
Reply all
Reply to author
Forward
0 new messages