1.獲取URL路徑中的參數
需求:假設用戶訪問127.0.0.1/user/1/2,你想獲取1,2。應該怎么操作呢?
(1)未命名參數(位置參數)
# 在項目下的urls.py下增加設置: url(r'^user/(\d+)/(\d+)$',views.index) # 在user.views的index視圖中: def index(request,a,b): # 接受的參數按順序的 return HttpResponse("獲得數據 %s %s"%(a,b))
(2)命名參數(關鍵字參數)
# 在項目下的urls.py下增加設置: url(r'^user/(?P<category>\d+)/(?P<id>\d+)$',views.index) # 在user.views的index視圖中: def index(request,id,category): # 接受的參數可以不用按順序的 return HttpResponse("獲得數據 %s %s"%(category,id))
輸出結果均是 獲得數據 1 2
2.獲取查詢字符串
需求:獲取127.0.0.1:8000/user?id=1&pid=99的查詢字符串的值
# 在項目下的urls.py下增加設置: url(r'^user/$',views.index) # 在user.views的index視圖中: def index(request): id = request.GET.get("id") pid = request.GET.get("pid") return HttpResponse("獲得數據 %s %s"%(id,pid))
注意:查詢字符串的獲取與請求方式無關:不管是 GET
還是 POST
請求,都可以通過request.GET
屬性來獲取!!!
3.獲取表單數據
用postman發送一個表單請求。
def index(request): id = request.POST.get("id") pid = request.POST.get("pid") return HttpResponse("獲得數據 %s %s"%(id,pid))
注意:request.POST
只能用來獲取POST方式的請求體表單數據!
4.獲取非表單類型
request.body
屬性:獲取非表單類型的請求體數據,如:JSON、XML等,獲取到的數據類型為bytes
類型- 獲取數據后,自己解析數據取出參數
def index(request): json_str = request.body json_str = json_str.decode() # python3.6及以上不用這一句代碼 dict_data = json.loads(json_str) # loads把str轉換為dict,dumps把dict轉換為str id = dict_data.get("id") pid = dict_data.get("pid") return HttpResponse("獲得數據 %s %s"%(id,pid))
5.獲取請求頭的內容
常見的請求頭如下:
CONTENT_LENGTH
– The length of the request body (as a string).CONTENT_TYPE
– The MIME type of the request body.HTTP_ACCEPT
– Acceptable content types for the response.HTTP_ACCEPT_ENCODING
– Acceptable encodings for the response.HTTP_ACCEPT_LANGUAGE
– Acceptable languages for the response.HTTP_HOST
– The HTTP Host header sent by the client.HTTP_REFERER
– The referring page, if any.HTTP_USER_AGENT
– The client’s user-agent string.QUERY_STRING
– The query string, as a single (unparsed) string.REMOTE_ADDR
– The IP address of the client.REMOTE_HOST
– The hostname of the client.REMOTE_USER
– The user authenticated by the Web server, if any.REQUEST_METHOD
– A string such as"GET"
or"POST"
.SERVER_NAME
– The hostname of the server.SERVER_PORT
– The port of the server (as a string).
獲取請求頭內容的用META
示例:
def index(request): ip = request.META.get("REMOTE_ADDR") return HttpResponse("你的ip地址是%s"%ip)
6.獲取自定義請求頭的內容
用postman增加一個自定義的請求頭,key=id,value=1。那么應該怎么取呢?
代碼如下:
def index(request): id = request.META.get("HTTP_ID") return HttpResponse("你的id:%s"%id)
注意:獲取自定義的請求頭屬性值時,需要添加前綴 HTTP_
並轉成大寫,作為鍵來獲取值
附錄(獲取各種請求參數的方法):
Attribute | Description |
---|---|
path | 請求頁面的全路徑,不包括域名端口參數。例如: /users/index |
method | 一個全大寫的字符串,表示請求中使用的HTTP方法,常用值:GET , POST ,DELETE ,PUT 等。以下三種為 GET 請求:
|
user |
|
GET | 類似字典的 QueryDict 對象,包含url中所有的查詢字符串參數 |
POST | 類似字典的 QueryDict 對象,包含 POST 請求的所有鍵值對參數(表單數據) |
body | 獲取原始的請求體數據,獲取到的數據為bytes類型 |
FILES | 一個類似於字典的對象,包含所有的上傳文件 |
META | python 字典類型,封裝了請求頭headers中的數據- REMOTE_ADDR – 客戶端的IP地址 - REQUEST_METHOD — 一個字符串,例如"GET" 或"POST- CONTENT_TYPE – 請求的正文的MIME 類型注意:對於用戶添加到請求頭中的鍵值,Django會給鍵加上前綴 HTTP_ 再轉換成大寫,再把鍵值保存到request.META中 |
COOKIES | 一個標准的 python 字典,包含所有的 cookies , 鍵和值都是字符串 |
session | 可讀可寫的類似字典的對象: django.contrib.sessions.backends.db.SessionStore 。Django 提供了 session 模塊,默認就會開啟用來保存 session 數據 |