16

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?

LolPython
  • 168
  • 1
  • 11
Bootstrap4
  • 3,671
  • 4
  • 13
  • 17
  • 2
    `request.GET` will hold a dictionary and the `.get('page', 1)` is the call on that dictionary to retrieve the value from that dictionary with key 'page'. If key 'page' does not exist, return 1 instead. – idjaw Jun 16 '17 at 22:51
  • The [documentation](https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.GET) explains this to very clearly. – idjaw Jun 16 '17 at 22:53
  • how about href=?page – Bootstrap4 Jun 16 '17 at 23:01
  • 1
    read [here](https://stackoverflow.com/questions/4855168/what-is-href-and-why-is-it-used). You should hit up some web development and HTML tutorials. – idjaw Jun 16 '17 at 23:02

3 Answers3

34

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.

Jessie
  • 2,319
  • 1
  • 17
  • 32
  • how does href=?page relate to therequest.GET – Bootstrap4 Jun 16 '17 at 23:03
  • 1
    If your URL contains `?page` then you've defined a variable called page, but it will have a value of `None`. If you wanted to do view the 3rd page, you would say `?page=3`. – Jessie Jun 17 '17 at 14:17
11

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
Ru Chern Chong
  • 3,692
  • 13
  • 33
  • 43
Ashwin Saji
  • 111
  • 1
  • 3
1

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