# Adding a template tag to generate query strings

**URL:** <https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521>\
**Category:** Django Internals\
**Created:** [October 14, 2023, 4:46pm UTC](https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521 "2023-10-14T16:46:00Z")\
**Posts on this page:** 3\
**Page:** 3

<div class="post-metadata">

**Author:** ![nessita](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nessita/32/12194_2.png) [@nessita](https://forum.djangoproject.com/u/nessita)\
**Post date:** [July 15, 2024, 7:04pm UTC](https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521/41 "2024-07-15T19:04:16Z")

</div>

Hello again!

We have landed the template tag rename and I have started work on:

1. Some polishing of existing docs in `ref`. I think it’s not explicit in the initial description of the tag that you can pass `foo=val` as argument, a first read suggest that only a `QueryDict` or `request.GET` will be accepted.
2. Reviewing [PR #18368](https://github.com/django/django/pull/18368) which adds supports for passing a dict as parameter.

I was aiming at having both items ready for the 5.1RC release. However, @sarahboyce rightly pointed out that the second item may need to be treated as a 5.2 improvement, with its corresponding release note, as it’s not strictly a release blocker.

But, I think that a common use case for this template tag will be passing dictionaries to create a properly formatted and encoded query string. Currently, creating a `QueryDict` from a dictionary feels cumbersome, especially since I couldn’t find a straightforward method to do so. It seems the options are either using `urlencode` on the dictionary and then passing the resulting string to `QueryDict`, or initializing an empty `QueryDict` with `mutable=True` and updating it with the desired dictionary.

Because of the above, I propose that we do include [PR #18368](https://github.com/django/django/pull/18368) changes in 5.1 as a (sort of?) release blocker to improve and streamline the usage of this template tag.

Thoughts?

---

<div class="post-metadata">

**Author:** ![apollo13](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/apollo13/32/30_2.png) [@apollo13](https://forum.djangoproject.com/u/apollo13)\
**Post date:** [July 15, 2024, 8:03pm UTC](https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521/42 "2024-07-15T20:03:21Z")

</div>

In my (limited) experience the main usecase is passing `request.GET` and altering parameters there. The common example being having `/users?page=3` and toggling `&sort=username` etc… If you need to change more than one field, having a form which submits properly covers the remaining cases (though I admit that I might miss something here, but those are my personal usecases so far).

As such I am fine with not having (2.) in 5.1 – but I have no string feelings.

---

<div class="post-metadata">

**Author:** ![carstenfuchs](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carstenfuchs/32/1139_2.png) [@carstenfuchs](https://forum.djangoproject.com/u/carstenfuchs)\
**Post date:** [July 16, 2024, 5:17am UTC](https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521/43 "2024-07-16T05:17:52Z")

</div>

Hello,

> [@apollo13](#):
>
> If you need to change more than one field, having a form which submits properly covers the remaining cases

A simple example for using `dict`s along with forms is `[<]` and `[>]` buttons like these:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/b/b/bbdff7b5759e93cc19b0ba01e3802afb558d5ea0.png)

The form is constructed from the view’s query parameters. The two buttons are “quick actions”, that is, links to the same view that must share the same parameters as the form but override the month and year with precomputed values for the previous and next time period:

```auto
{% querystring request.GET prev_period_month_and_year %}

```

So, personally I think it would be helpful to have this in 5.1. 😜

[Previous page](https://forum.djangoproject.com/t/adding-a-template-tag-to-generate-query-strings/24521.md?page=2)
