django中的request物件詳解
原文參考:
Request
我們知道當URLconf檔案匹配到使用者輸入的路徑後,會呼叫對應的view函式,並將 HttpRequest物件 作為第一個引數傳入該函式。
Django 每一個view函式的第一個引數都是request,有沒想過request裡面到底有什麼呢?
Django使用request和response物件在系統間傳遞狀態。
當一個頁面被請示時,Django建立一個包含請求元資料的 HttpRequest 物件。 然後Django調入合適的檢視,把HttpRequest 作為檢視函式的第一個引數 傳入。每個檢視要負責返回一個 HttpResponse 物件。
HttpRequest例項的屬性包含了關於此次請求的大多數重要資訊。 除了session外的所有屬性都應該認為是隻讀的.
我們來看一看這個HttpRequest物件有哪些屬性或者方法:
屬性:
1 HttpRequest.scheme
請求的協議,一般為http或者https,字串格式(以下屬性中若無特殊指明,均為字串格式)
2 HttpRequest.body
http請求的主體,二進位制格式。
3 HttpRequest.path
所請求頁面的完整路徑(但不包括協議以及域名),也就是相對於網站根目錄的路徑。
4 HttpRequest.path_info
獲取具有 URL 副檔名的資源的附加路徑資訊。相對於HttpRequest.path,使用該方法便於移植。
if the WSGIScriptAlias for your application is set to "/minfo", then path might be "/minfo/music/bands/the_beatles/" and path_info would be "/music/bands/the_beatles/".
5 HttpRequest.method
獲取該請求的方法,比如: GET POST .........
6 HttpRequest.encoding
獲取請求中表單提交資料的編碼。
7 HttpRequest.content_type
獲取請求的MIME型別(從CONTENT_TYPE頭部中獲取),django1.10的新特性。
8 HttpRequest.content_params
獲取CONTENT_TYPE中的鍵值對引數,並以字典的方式表示,django1.10的新特性。
9 HttpRequest.GET
返回一個 querydict 物件(類似於字典,本文最後有querydict的介紹),該物件包含了所有的HTTP GET引數
10 HttpRequest.POST
返回一個 querydict ,該物件包含了所有的HTTP POST引數,通過表單上傳的所有 字元 都會儲存在該屬性中。
11 HttpRequest.COOKIES
返回一個包含了所有cookies的字典。
12 HttpRequest.FILES
返回一個包含了所有的上傳檔案的 querydict 物件。通過表單所上傳的所有 檔案 都會儲存在該屬性中。key的值是input標籤中name屬性的值,value的值是一個UploadedFile物件
13 HttpRequest.META
返回一個包含了所有http頭部資訊的字典
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).
14 HttpRequest.session
中介軟體屬性
15 HttpRequest.site
中介軟體屬性
16 HttpRequest.user
中介軟體屬性,表示當前登入的使用者。
HttpRequest.user實際上是由一個定義在django.contrib.auth.models 中的 user model 類 所建立的物件。
該類有許多欄位,屬性和方法。列舉幾個常用的: 獲取更詳細資訊-->官方文件。
1 欄位:
username 使用者名稱
first_name
last_name
password
groups
user_permissions,
is_staff 布林值,標明使用者是否可以訪問admin頁面
is_superuser
last_login 上一次登陸時間
date_joined 使用者建立時間
2 屬性
is_authenticated 布林值,標誌著使用者是否已認證。在django1.10之前,沒有該屬性,但有與該屬性同名的方法。
3 方法
1 HttpRequest.user.get_username() 注意:方法的圓括號在templates標籤中必需省略!!
獲取username。儘量使用該方法來代替使用username欄位
2 HttpRequest.user.get_full_name() 注意:方法的圓括號在templates標籤中必需省略!!
獲取first_name和last_name
3 HttpRequest.user.short_name() 注意:方法的圓括號在templates標籤中必需省略!!
獲取first_name
4 HttpRequest.user.set_password(raw_password) 注意:該方法無法在template標籤中使用!!
設定密碼
5 HttpRequest.user.check_password(raw_password) 注意:該方法無法在template標籤中使用!!
如果raw_password與使用者密碼相等,則返回True
方法:
1 HttpRequest.get_host()
返回請求的源主機。example: 127.0.0.1:8000
2 HttpRequest.get_port()
django1.9的新特性。
3 HttpRequest.get_full_path()
返回完整路徑,幷包括附加的查詢資訊。example:
"/music/bands/the_beatles/?print=true"
4 HttpRequest.bulid_absolute_uri(location)
返回location的絕對uri,location預設為request.get_full_path()。
Example: "https://example.com/music/bands/the_beatles/?print=true"
QueryDict
是一個類似於Python中字典的一種物件,他是Python中字典的子類,所以繼承了字典的所有方法,
當然QueryDict對字典的某些方法進行了加工,並補充了一些獨特的方法。這裡列出部分方法。詳情請看: 官方文件 。
1 QueryDict.get(key,default=None) 返回key所對應的value,若key不存在,則返回default的值
2 QueryDict.update(other_dict) 更新
3 QueryDict.values() 列出所有的值
4 QueryDict.items() 列出所有的鍵值對,若一個key有多個值,只顯示最後一個值。
5 QueryDict.pop(key) 刪除某個鍵值對
6 QueryDict.getlist(key) 根據輸入的key返回一個Python中的list
7 QueryDict.dict() 返回QueryDict的字典的表現形式