Opened 5 months ago

Closed 5 months ago

#35523 closed Bug (wontfix)

query_string cannot use variables as keys

Reported by: Sarah Boyce Owned by: nobody
Component: Template system Version: 5.1
Severity: Release blocker Keywords:
Cc: Tom Carrick, Florian Apolloner Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Raised by Florian here: https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521/15

If we are to add a test:

  • tests/template_tests/syntax_tests/test_query_string.py

    a b class QueryStringTagTests(SimpleTestCase):  
    9898        msg = "'Context' object has no attribute 'request'"
    9999        with self.assertRaisesMessage(AttributeError, msg):
    100100            self.engine.render_to_string("query_string_no_request_no_query_dict")
     101
     102    @setup(
     103        {
     104            "query_string_var_key": '{% with key="var_key" %}{% query_string key=2 %}'
     105            "{% endwith %}"
     106        }
     107    )
     108    def test_query_string_with_variable_key(self):
     109        request = self.request_factory.get("/")
     110        template = self.engine.get_template("query_string_var_key")
     111        context = RequestContext(request)
     112        output = template.render(context)
     113        self.assertEqual(output, "?var_key=2")

This fails with AssertionError: '?key=2' != '?var_key=2'.

One suggestion is to quote literal keys eg. {% query_string var_key=1 'other_key'=2 %}

Refs #10941, design decision required, potential release blocker

Change History (2)

comment:1 by Carsten Fuchs, 5 months ago

This is why my original code supported dictionaries for input.
See the last example at https://code.djangoproject.com/ticket/10941#comment:33, repeated here for reference:

{# 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 %}
Version 0, edited 5 months ago by Carsten Fuchs (next)

comment:2 by Sarah Boyce, 5 months ago

Resolution: wontfix
Status: newclosed

Based off the conclusion in the discussion

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