Opened 8 days ago

Last modified 7 days ago

#35529 new Cleanup/optimization

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

Reported by: Sarah Boyce Owned by: nobody
Component: Template system Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
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 (8)

comment:1 by Sarah Boyce, 8 days ago

Description: modified (diff)

comment:2 by Sarah Boyce, 8 days ago

Description: modified (diff)

comment:3 by Carsten Fuchs, 8 days 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, 8 days 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, 8 days 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, 8 days 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, 8 days 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, 7 days ago

Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Accepting following the forum conversation.

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