From 1fa5d28ead1d20abfd2416d46448687ba000ff8c Mon Sep 17 00:00:00 2001 From: Taylor Hodge Date: Mon, 2 Sep 2024 14:01:40 -0700 Subject: [PATCH] feat: add django querystring template tag til --- django/querystring-template-tag.md | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 django/querystring-template-tag.md diff --git a/django/querystring-template-tag.md b/django/querystring-template-tag.md new file mode 100644 index 0000000..e678236 --- /dev/null +++ b/django/querystring-template-tag.md @@ -0,0 +1,31 @@ +# `{% querystring %}` Template Tag + +Django 5.1 introduces [a new template tag](https://docs.djangoproject.com/en/5.1/ref/templates/builtins/#std-templatetag-querystring): `{% querystring %}`. + +This tag output a URL-encoded, formatted query string based on the provided parameters. E.g.: + +```html +{% querystring color="blue" size="L" %} +``` + +Appends the `?color=blue&size=L` query params to the current URL..._while_ keeping any existing +parameters, and replacing the current value for `color` or `size` if the value's already set + +```html +{% querystring color=None %} +``` + +Removes the `?color=` query param from the current URL, if it's already set. + +And, if the value passed is a list, then it will append `?color=blue&color=green` to the current URL, for as many items as exist in the list. + +```html +{% querystring page=page.next_page_number as next_page %} +``` + +Further, you can access values in the variables. And, you can also assign the result to a +new template variable rather than outputting it directly to the page: + +```html +{% querystring page=page.next_page_number as next_page %} +```