Django
GET request
Django views
HTTP request handling
web development

How to get GET request values in Django?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

In Django, query-string values from a GET request are available through request.GET. This object is a QueryDict, which behaves a lot like a dictionary but also supports repeated keys, making it the right place to read values such as search terms, filters, page numbers, and other URL parameters.

The Basic Pattern

Suppose the browser requests a URL like this:

/search/?q=django&page=2&tag=web&tag=python

Inside the view, you can read those parameters directly.

python
1from django.http import JsonResponse
2
3
4def search(request):
5    query = request.GET.get("q", "")
6    page = int(request.GET.get("page", "1"))
7    tags = request.GET.getlist("tag")
8
9    return JsonResponse({
10        "query": query,
11        "page": page,
12        "tags": tags,
13    })

get() returns one value or a default if the key is missing. getlist() returns all values for repeated keys.

Why QueryDict Matters

A regular Python dictionary can only store one value per key. Query strings can repeat the same key many times, which is why Django uses QueryDict.

That means these methods have distinct meanings:

  • 'request.GET["q"] raises an error if the key is missing.'
  • 'request.GET.get("q") safely returns one value or None.'
  • 'request.GET.getlist("tag") returns every matching value.'

For filter-heavy endpoints, getlist() is often the difference between correct and incomplete behavior.

GET Versus POST

A common beginner confusion is mixing request.GET and request.POST. These are separate data sources.

  • Use request.GET for query-string parameters in the URL.
  • Use request.POST for form data submitted in the request body.

The HTTP method of the request matters, but the container also matters. A POST request can still have query-string values in request.GET if they are present in the URL.

Handle Missing Or Invalid Values Safely

Treat GET parameters as user input. That means validating types and providing defaults.

For example, converting page to an integer without a fallback can raise ValueError if the user sends page=abc. If the parameter is important, validate it explicitly and return a clear response instead of letting the view crash.

Copying And Modifying Values

request.GET is immutable. If you need to modify it, create a copy first:

python
params = request.GET.copy()
params["page"] = "1"

That is useful when normalizing or reusing parameters for redirects or pagination links.

Query parameters also show up frequently in pagination and filtering links rendered by templates. Keeping the view logic based on request.GET makes those URLs shareable and repeatable, which is one of the main reasons GET parameters are so useful for search pages and list views in the first place.

The immutability of request.GET is also useful because it keeps incoming request data separate from any normalized version you build in your own code. If you need to rewrite or supplement parameters, use copy() first so the original request values remain available for logging, debugging, or comparison.

For pagination links and filter-preserving redirects, Django also lets you re-encode parameter dictionaries cleanly once you have copied or normalized them. That becomes useful when a view needs to keep existing search parameters while adding a new page number or sort option.

Common Pitfalls

One common mistake is using request.GET["key"] everywhere and then getting avoidable exceptions when parameters are missing. get() is usually safer for optional values.

Another mistake is forgetting that repeated keys require getlist(). Using get() on a multi-select filter silently drops all but one value.

A third issue is assuming GET values are already trustworthy. They are user-controlled input and still need validation and sanitization for your application logic.

Summary

  • In Django, read query-string parameters from request.GET.
  • Use get() for single optional values and getlist() when a key may appear multiple times.
  • Remember that request.GET is a QueryDict, not a plain dictionary.
  • Validate GET parameters just like any other user input before relying on them.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.