Opened 6 months ago

Last modified 4 months ago

#35529 assigned Cleanup/optimization

Have the template tag query_string support multiple arguments and arguments of type Mapping[str, Any]

Reported by: Sarah Boyce Owned by: Giannis Terzopoulos
Component: Template system Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Sarah Boyce)

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

Currently query_string only supports a single QueryDict as an argument, consensus appears to want the following updates:

  • support arguments of type Mapping[str, Any]
  • support multiple arguments

Tests which hopefully illustrate the requested updates

  • tests/template_tests/syntax_tests/test_query_string.py

    a b class QueryStringTagTests(SimpleTestCase):  
    9393        )
    9494        self.assertEqual(output, "?a=2&b=2")
    9595
     96    @setup({"query_string_dict": "{% query_string my_dict %}"})
     97    def test_query_string_with_explicit_dict_and_no_request(self):
     98        context = {"my_dict": {"a": 1, "b": 2}}
     99        output = self.engine.render_to_string("query_string_dict", context)
     100        self.assertEqual(output, "?a=1&b=2")
     101
     102    @setup({"query_string_multiple_args": "{% query_string my_dict my_query_dict %}"})
     103    def test_query_string_with_multiple_args(self):
     104        context = {"my_dict": {"a": 1, "b": 2}, "my_query_dict": QueryDict("c=1")}
     105        output = self.engine.render_to_string("query_string_multiple_args", context)
     106        self.assertEqual(output, "?a=1&b=2&c=1")
     107
    96108    @setup({"query_string_no_request_no_query_dict": "{% query_string %}"})
    97109    def test_query_string_without_request_or_explicit_query_dict(self):

Change History (12)

comment:1 by Sarah Boyce, 6 months ago

Description: modified (diff)

comment:2 by Sarah Boyce, 6 months ago

Description: modified (diff)

comment:3 by Carsten Fuchs, 6 months ago

Isn't this how it is already implemented?

My understanding of the discussion was that more than one dict could be supported.

in reply to:  3 comment:4 by Sarah Boyce, 6 months ago

Replying to 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'

comment:5 by Carsten Fuchs, 6 months ago

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.

in reply to:  5 comment:6 by Sarah Boyce, 6 months ago

Replying to 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 👍

comment:7 by Sarah Boyce, 6 months ago

Description: modified (diff)
Summary: Have the template tag query_string support Mapping[str, Any] as an argumentHave the template tag query_string support multiple arguments and arguments of type Mapping[str, Any]

comment:8 by Natalia Bidart, 6 months ago

Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Accepting following the forum conversation.

comment:9 by Giannis Terzopoulos, 6 months ago

Owner: changed from nobody to Giannis Terzopoulos
Status: newassigned

comment:10 by Giannis Terzopoulos, 5 months ago

Has patch: set

comment:11 by Sarah Boyce, 5 months ago

Patch needs improvement: set

comment:12 by Natalia Bidart, 4 months ago

PR looks good but I found a few cases where it could be considered that we are breaking previous behavior. I have proposed PR 18487 to add extra tests for the existing tag as is. Leaving "patch needs improvement" set.

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