Python Django Live Projects for sell
Hi,
We have few Python Django live projects which are available for sell. Please visit this link for more details.
Advertise with us
requests django   1   3427
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   3427

Related Articles:
How to reset Django superuser password
This article explains 3 methods to reset the user password in Django, What command should be used to reset the superuser password from the terminal in Django application, Changing the user password in Django...
Encryption-Decryption in Python Django
How to encrypt and decrypt the content in Django, Encrypting the critical information in Django App, Encrypting username, email and password in Django, Django security...
How to upload an Image file in Django
This article explains the simple steps of uploading and storing an image in Django application, After storing the image, how to use it in Django template or emails, Uploading a file in Django, Storing image in Django model, Uploading and storing the image in Django model, HTML for multipart file upload...
How to setup Django app on EC2 instance (AWS)
Step by step guide on hosting Django application on AWS ec2 instance, How to host the Django app on AWS ec2 instance from scratch, Django on EC2, Django app hosting on AWS, Free hosting of Django App...

1 thought on 'Getting Query Params From Request In Django'
Catalin George Festila :
very good article... django is a good framework... do you use asyncio wirh django ?

Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.

SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap