requests django   1   51276
Getting query params from request in Django


To get query parameters from the request in the Django view, you need to access the GET attribute of the request.

def get_request_example(request):
print(request.GET)
data = dict()
return render(request, 'hw/home.html', data)


To demonstrate this, we will extend our previous example of the hello world app in Django. Source code is available on GitHub. Clone this repository on your system.


Add the below path in app hw's urls.py file.

path(r'get-request-example/', views.get_request_example, name='get_request_example'),


Add below function in views.py file.

from django.shortcuts import render

def get_request_example(request):
print(request.GET)
return render(request, 'hw/home.html', {})


Now restart the Django development server using command python manage.py runserver

Open the web browser and go to the URL localhost:8000/get-request-example/. Since we are printing query parameters in view, we will see an empty QueryDict getting printed in the terminal.

Django version 2.2.9, using settings 'helloworld.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
<QueryDict: {}>
[15/Feb/2020 07:36:53] "GET /get-request-example/ HTTP/1.1" 200 219

Now change the URL to /get-request-example?param1=value1. You will see below logs in the terminal.

<QueryDict: {'param1': ['value1']}>
[15/Feb/2020 11:16:45] "GET /get-request-example/?param1=value1 HTTP/1.1" 200 219


Let try to pass multiple params in URL. Change the URL to /get-request-example?param1=value1&param2=value2. You will see below logs in the terminal.

<QueryDict: {'param2': ['value2'], 'param1': ['value1']}>
[15/Feb/2020 11:20:48] "GET /get-request-example/?param1=value1&param2=value2 HTTP/1.1" 200 219

Each key in QueryDict will have a list of values against it. Try changing the URL to /get-request-example?param1=value1&param1=value2. Note that we are passing parameter param1 twice with different values.

<QueryDict: {'param1': ['value1', 'value2']}>
[15/Feb/2020 11:22:08] "GET /get-request-example/?param1=value1&param1=value2 HTTP/1.1" 200 219


As you can see, param1 has a list of two values against it.



Accessing a parameter from QueryDict:

We can get the value of any parameter using the get() method of the python dictionary.

print(request.GET.get("param1"))

Now hit the above URL again. Let's see what is getting logged in the terminal.

<QueryDict: {'param1': ['value1', 'value2']}>
value2
[15/Feb/2020 11:27:06] "GET /get-request-example/?param1=value1&param1=value2 HTTP/1.1" 200 219


What is this? Why only one value is getting printed? 

So turns out we need to use the getlist() method to get a complete list of values.

print(request.GET.getlist("param1"))
<QueryDict: {'param1': ['value1', 'value2']}>
['value1', 'value2']
[15/Feb/2020 11:28:47] "GET /get-request-example/?param1=value1&param1=value2 HTTP/1.1" 200 219



Conclusion:

We can access the query params from the request in Django from the GET attribute of the request. To get the first or only value in a parameter simply use the get() method. To get the list of all values in a parameter use getlist() method.



Source code:

The updated source code for this tutorial is present on GitHub



Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.


requests django   1   51276
1 comment on 'Getting Query Params From Request In Django'
Login to comment

Catalin George Festila Feb. 16, 2020, 12:33 p.m.
very good article... django is a good framework... do you use asyncio wirh django ?

Related Articles:
How to post messages to Microsoft teams channel using Python
In this article we will see how to send alerts or messages to microsoft teams channels using connectors or incoming webhook. we used python's requests module to send post request....
Python Requests Library: Sending HTTP GET and POST requests using Python
Python requests library to send GET and POST requests, Sending query params in Python Requests GET method, Sending JSON object using python requests POST method, checking response headers and response status in python requests library...
Sending post request with different body formats in Django
There are a few different formats of the body we can use while sending post requests. In this article, we tried to send post requests to different endpoints of our hello world Django application from the postman. Let's see what is the difference between discussed body formats....
Scraping data of 2019 Indian General Election using Python Request and BeautifulSoup and analyzing it
scraping 2019 election data india, Scraping data of 2019 Indian General Election using Python BeautifulSoup and analyzing it, using beautifulsoup to collection election data, using request...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap