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¶m2=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¶m2=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¶m1=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¶m1=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¶m1=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¶m1=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.