简体   繁体   English

request.GET.get是什么意思?

[英]What does request.GET.get mean?

What does request.GET.get mean? request.GET.get是什么意思? I see something like this in Django 我在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? 他们是如何工作的?

The request object contains information about the user's request. request对象包含有关用户请求的信息。 What data they've sent to the page, where they are coming from, etc. 他们发送到页面的数据,他们来自哪里等等。

request.GET contains the GET variables. request.GET包含GET变量。 These are what you see in your browser's address bar. 这些是您在浏览器的地址栏中看到的内容。 The .get() method is a method used for dictionaries. .get()方法是用于字典的方法。 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". 您的代码片段正在做的是“获取名称为'page'的GET变量的值,如果它不存在,则返回1”。

Likewise, you will see request.POST used when a user submits a form. 同样,您将看到用户提交表单时使用的request.POST

You can read more about GET vs. POST here . 您可以在此处阅读有关GET与POST的更多信息。

request.GET is the dictionary of the GET variables in the http request made to your server for example: request.GET是对服务器发出的http请求中的GET变量的字典,例如:

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

request.GET would be: {"thisIsAGetVarKey": 3, "thisIsAnotherOne":"hello"} request.GET将是: {"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 因为request.GET是一个字典,所以它有方法.get() ,它检索字典中键的值

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

request.GET is the dictionary of the 'GET' variables in the http request made to your server for example: request.GET是对服务器发出的http请求中的'GET'变量的字典,例如:
www.google.com?thisIsAGetVarKey=3&thisIsAnotherOne=hello

request.GET would be: {"thisIsAGetVarKey": 3, "thisIsAnotherOne":"hello"} request.GET将是: {"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 - 因为request.GET是一个字典,它有方法.get(),它检索字典中键的值 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM