Request
我們知道當URLconf文件匹配到用戶輸入的路徑后,會調用對應的view函數,並將 HttpRequest對象 作為第一個參數傳入該函數。
我們來看一看這個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的字典的表現形式