request.GET.get是什么意思?

Boo*_*ap4 3 python django http

request.GET.get是什么意思?我在Django看到类似的东西

page = request.GET.get('page', 1)
Run Code Online (Sandbox Code Playgroud)

我认为这与某些事情有关

<li><a href="?page={{ users.previous_page_number }}">&laquo;</a></li>
Run Code Online (Sandbox Code Playgroud)

他们是如何工作的?

Jes*_*sse 11

request对象包含有关用户请求的信息.他们发送到页面的数据,他们来自哪里等等.

request.GET包含GET变量.这些是您在浏览器的地址栏中看到的内容.该.get()方法是用于字典的方法.您的代码片段正在做的是"获取名称为'page'的GET变量的值,如果它不存在,则返回1".

同样,您将看到request.POST用户提交表单时使用的内容.

您可以在此处阅读有关GET与POST的更多信息.


小智 5

request.GETGET服务器发出的http请求中变量的字典,例如:

www.google.com?thisIsAGetVarKey=3&thisIsAnotherOne=hello
Run Code Online (Sandbox Code Playgroud)

request.GET 将会: {"thisIsAGetVarKey": 3, "thisIsAnotherOne":"hello"}

因为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
Run Code Online (Sandbox Code Playgroud)