---
title: '{% querystring %}'
description: outputs a URL-encoded query string.
date: '2026-08-02'
categories:
  - Template Tag
  - Utility
canonical: https://www.djangotemplatetagsandfilters.com/tags/querystring/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#querystring
---

# {% querystring %}

outputs a URL-encoded query string.

## Documentation

Outputs a URL-encoded query string based on the provided parameters. The result always includes a leading `?`, making it suitable for direct use in URLs.

### Arguments

- **Positional arguments** – Mappings like `QueryDict` or `dict` instances.
- **Keyword arguments** – Key-value pairs to add or modify parameters.

If no positional arguments are provided, `request.GET` is used as the default source.

### Basic Usage

Add or replace parameters in the current query string:

### Template

```django
<a href="{% querystring color='red' size='S' %}">Red, Small</a>
```

### Result

```django
<a href="?color=red&size=S">Red, Small</a>
```

### Modifying the Current Query String

When used without positional arguments, it modifies the current `request.GET`:

### Template

```django
{# If current URL is ?color=blue&size=M #}
<a href="{% querystring size='L' %}">Large</a>
```

### Result

```django
<a href="?color=blue&size=L">Large</a>
```

### Removing Parameters

Pass `None` to remove a parameter:

### Template

```django
{# If current URL is ?color=blue&size=M #}
<a href="{% querystring color=None %}">Clear color filter</a>
```

### Result

```django
<a href="?size=M">Clear color filter</a>
```

### Handling Lists

If a value is a list, multiple parameters with the same key are output:

### Variable

```django
colors = ['red', 'blue']
```

### Template

```django
<a href="{% querystring color=colors %}">Red and Blue</a>
```

### Result

```django
<a href="?color=red&color=blue">Red and Blue</a>
```

### Storing in a Variable

Use `as` to store the result in a variable:

### Template

```django
{% querystring page=page_obj.next_page_number as next_page_url %}
<a href="{{ next_page_url }}">Next</a>
```

### Pagination Example

A common use case is pagination links that preserve other query parameters:

### Template

```django
{# Preserves filters like ?category=books while changing page #}
{% if page_obj.has_previous %}
  <a href="{% querystring page=page_obj.previous_page_number %}">Previous</a>
{% endif %}

{% if page_obj.has_next %}
  <a href="{% querystring page=page_obj.next_page_number %}">Next</a>
{% endif %}
```

## Commentary

This is a fantastic addition to Django's template tags. Before `querystring`, preserving existing query parameters while modifying one or two was tedious and error-prone.

The most common use case is pagination. Previously, you had to manually reconstruct the query string or use a custom template tag. Now it's built in.

**Note:** Requires the `django.template.context_processors.request` context processor to be enabled (it is by default in new Django projects).
