In this Django tutorial, you will learn how to get data from get request in Django.
When you send a request to the server, you can also send some parameters. Generally, we use a GET request to get some data from the server. We can send parameters with the request to get some specific data.
For example, you can think of an e-commerce website where you can see a list of products. Now to see the details of a specific product, you send the id associated with that product as a GET parameter.
These GET parameters are parsed at the server and the server returns a response page showing the details of that particular product.
You can parse the GET parameters from the request.GET object in Python Django. You have to define the name of the HTML control inside this object to parse the value. For example, you can use an HTML textbox to send a parameter.
In this article, you will see some examples of how GET parameters work in Django.
- Django get data from get request example
- Add two numbers in django
Django get data from get request example
Now I will start with a simple example. I have created a simple HTML page where I will take input using a textbox. The HTML source file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<div>
<form action = "result">
Enter your parameter : <input type="text" name="result" placeholder="Your input value"><br><br>
<input type="submit">
</form><br>
</div>
</body>
</html>
Now this HTML form will redirect the request to the result endpoint. I have defined this endpoint in the urls.py file of the application. The urls.py file is:
from django.urls import path
from . import views
urlpatterns = [
path('',views.index),
path('result', views.result)
]
This endpoint will execute the result function in the view.py file. You can see the views.py file below.
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
def result(request):
result = request.GET['result']
return render(request, 'result.html', {'result':result})
You can see in the file how I have implemented the result function to parse the GET parameter. After parsing the parameter, I have returned it to result.html template that will be rendered when the function is executed.
The above is the output of the first HTML page that I mentioned above.
When the Submit button is clicked, the request is made to the result endpoint along with the GET parameter as shown below in the image:
You can see that the value in the GET parameter got rendered.
In this way, you can get the value of a GET parameter in Django.
Add two numbers in django
Now let me show you how to add two numbers where I will add two GET parameters received from a request in Django.
For this, first of all, I will need an HTML page, where I will place my input controls to get data. Also, I will return the result on the same page. The HTML file is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<div>
<form action = "sum">
Enter First Number: <input type="text" name="first" placeholder="First Number"><br><br>
Enter Second Number: <input type="text" name="second" placeholder="Second Number"><br><br>
<input type="submit">
</form><br>
</div>
</body>
</html>
You can see that I have redirected this page to the sum endpoint. Now I need to configure the urls.py file to define the path and the function mapping.
from django.urls import path
from . import views
urlpatterns = [
path('',views.index),
path('sum', views.sum1)
]
You can see in the above file that the sum1() function in the views.py file will be executed when a request is made to the sum endpoint.
Now the views.py file looks like this:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
def sum1(request):
num1 = int(request.GET['first'])
num2 = int(request.GET['second'])
result = num1 + num2
return render(request, 'sum.html', {'result':result})
Here you can see how I have parsed the GET parameters and returned their sum to the sum.html template using the sum1() function.
You can parse the GET parameters from the request.GET object. You have to define the name of the input control inside this object to parse the value.
Now let us see the execution of this application.
- First of all, I will make a request to the root endpoint:
- Then I will input two numbers and click on Submit.
- Clicking on the Submit button will send a GET request to the sum endpoint along with the two parameters. You can see the GET parameters in the URL.
- The application will return the result as shown in the below image:
Hence, with this example, you might have learned how GET parameters work in a Django application.
You may like the following Django tutorials:
- Python Django length filter
- Python Django set timezone
- Python Django format date
- Python Change Django Version
- If statement in Django template
- Get URL parameters in Django
- How to Get Current time in Django
- Python Django vs Pyramid
Thus, you might have learned how you can get the data from a GET request as a parameter.
- Django get data from get request example
- Add two numbers in django
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.