Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does request.GET.get mean?

What does request.GET.get mean? I see something like this in Django

page = request.GET.get('page', 1)

which I think is connected to something like

<li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>

How do they work?

like image 873
Bootstrap4 Avatar asked Jun 16 '17 22:06

Bootstrap4


People also ask

What is returned by the request GET GET?

request. GET contains the GET variables. These are what you see in your browser's address bar.

What is request get in Python?

Python's requests module provides in-built method called get() for making a GET request to a specified URL. Syntax – requests.get(url, params={key: value}, args) Example – Let's try making a request to Github's APIs for example purposes. Python3.

What are request get and request post objects?

So, to request a response from the server, there are mainly two methods: GET : to request data from the server. POST : to submit data to be processed to the server.

What is request Get Django?

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.


3 Answers

request.GET is the dictionary of the GET variables in the http request made to your server for example:

www.google.com?thisIsAGetVarKey=3&thisIsAnotherOne=hello

request.GET would be: {"thisIsAGetVarKey": 3, "thisIsAnotherOne":"hello"}

Because request.GET is a dictionary, it has the method .get() which retrieves a value for a key in the dictionary

dict_b = {'number': 8, 'alphabet':'A'}
print dict_a['number'] #prints 8
print dict_a.get('alphabet') #prints A

print dict_a['bob'] #throws KeyError
print dict_a.get('bob') #prints None
print dict_a.get('bob', default=8) #prints 8
like image 84
Ashwin Saji Avatar answered Nov 11 '22 02:11

Ashwin Saji


The request object contains information about the user's request. What data they've sent to the page, where they are coming from, etc.

request.GET contains the GET variables. These are what you see in your browser's address bar. The .get() method is a method used for dictionaries. What your snippet of code is doing is saying, "Get the value of a GET variable with name 'page', and if it doesn't exist, return 1".

Likewise, you will see request.POST used when a user submits a form.

You can read more about GET vs. POST here.

like image 39
Jessie Avatar answered Nov 11 '22 03:11

Jessie


request.GET is the dictionary of the 'GET' variables in the http request made to your server for example:
www.google.com?thisIsAGetVarKey=3&thisIsAnotherOne=hello

request.GET would be: {"thisIsAGetVarKey": 3, "thisIsAnotherOne":"hello"}

Because request.GET is a dictionary, it has the method .get() which retrieves a value for a key in the dictionary -

dict_a = {'age': 3}
print dict_a['age'] #prints 3
print dict_a.get('a') #also prints 3

print dict_a['hello'] #throws KeyError
print dict_a.get('hello') #prints None
print dict_a.get('hello', default=3) #prints 3
like image 29
doratheexplorer0911 Avatar answered Nov 11 '22 01:11

doratheexplorer0911