How to get Query Params from Request in Django ?
Hi Dev,
This is a short guide on django get query params from request. if you have question about python django get url parameters example then I will give simple example with solution. We will use python django url parameters. Here you will learn python django get request parameters.
Here, we will also see how to display them in your template. But, before that, we should know about the GET method.
To get the URL parameters, we have to use the request.GET method. So, first, we will use this method to fetch and store the value of parameters in a variable. After this, we will return the variable to an HTML page. Let’s understand this with the help of an example.
Let's see bellow example here you will learn python django get request parameters.
Example : 1In this step, we need to create the views for performing get request url parameters.Open the views.py file and add:
views.pyfrom django.shortcuts import render def index(request): return render(request, 'index.html') def home(request): msg=request.GET.get('message') return render(request, 'home.html',{'msg':msg })
Here is the code for the index.html page.
<!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"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <title>Tuts-Station.com</title> </head> <body> <br> <div class="container w-50"> <div class="card bg-light text-dark"> <h5 class="card-header bg-primary text-light">Index Page</h5> <div class="card-body"> <div class="card-text"><a href="{% url 'home' %}?message=Hi Welcome to Tuts-Station.com">Link To Home Page</a></div> </div> </div> </div> </body> </html>
Here is the code for the home.html page.
<!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"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> <title>Tuts-Station.com</title> </head> <body> <br> <div class="container"> <div class="card bg-light text-dark"> <h5 class="card-header bg-primary text-light">Home Page</h5> <div class="card-body"> <div class="card-text"><b>URL Parameters: </b>{{msg}}</div> </div> </div> </div> </body> </html>Index Page

Home Page

I Hope It will help you....