-
class
HttpRequest[source]
屬性
所有的屬性都是只讀的,除非另有說明
-
HttpRequest.body
-
原始的http請求正文
也可以像文件一樣讀他,看
HttpRequest.read().
-
HttpRequest.path_info
-
在某些情況下,路徑被分割成腳本前綴和路徑,這樣可以使部署和測試更方便
例如腳本前綴為"/minfo", 路徑為"/minfo/music/bands/the_beatles/"path_info為"/music/bands/the_beatles/"
-
HttpRequest.method
-
字符串(GET/POST)表示GET還是POST請求
常用方法if request.method == 'GET': do_something() elif request.method == 'POST': do_something_else()
-
HttpRequest.encoding
-
一個字符串,用於解碼的當前編碼的表單提交的數據(或沒有,就使用thedefault_charset)。在訪問表單數據時,可以使用此屬性來更改所用的編碼。如果你知道表格數據不是default_charset編碼。隨后的任何屬性的訪問(如閱讀從GET或POST)將使用新的encodingvalue。
-
HttpRequest.GET
-
GET請求對象,詳細查看
QueryDict
-
HttpRequest.POST
-
POST請求對象,詳細查看
QueryDict注意:
POST不包含文件上傳,請看FILES.
-
HttpRequest.FILES
-
一個包含所有文件對象的字典. key是
<inputtype="file" name="" />中name的值,沒一個value是一個上傳的文件對象,請查看UploadedFile.參看 Managing files 獲取更多信息
如果要上傳文件需要在
<form>標簽中添加enctype="multipart/form-data",不然收到的是一個空值
-
HttpRequest.META
-
請求頭信息
例:
CONTENT_LENGTH請求體當作一個字符串的總長度。CONTENT_TYPE請求體的MIME類型。HTTP_ACCEPT 可以接受的內容類型的響應。HTTP_ACCEPT_ENCODING接受編碼的響應。HTTP_ACCEPT_LANGUAGE接受語言的反應。HTTP_HOST 客戶端請求時用的服務端地址HTTP_REFERER 參考頁面HTTP_USER_AGENT客戶端的標志信息QUERY_STRING一對一的查詢字符串REMOTE_ADDR客戶端IPREMOTE_HOST客戶端主機名REMOTE_USER客服端的身份信息REQUEST_METHOD請求方式SERVER_NAME服務器主機名SERVER_PORT 服務端開放的端口
-
HttpRequest.resolver_match
-
An instance of
ResolverMatchrepresenting the resolved url. This attribute is only set after url resolving took place, which means it’s available in all views but not in middleware methods which are executed before url resolving takes place (likeprocess_request, you can useprocess_viewinstead).
應用代碼設置的屬性
Django本身不設置這些屬性,可以被服務端設置的屬性
-
HttpRequest.current_app
-
Django 1.8后新屬性
The
urltemplate tag will use its value as thecurrent_appargument toreverse().
-
HttpRequest.urlconf
-
This will be used as the root URLconf for the current request, overriding the
ROOT_URLCONFsetting. See How Django processes a request for details.urlconfcan be set toNoneto revert any changes made by previous middleware and return to using theROOT_URLCONF.Changed in Django 1.9:Setting
urlconf=NoneraisedImproperlyConfiguredin older versions.
被中間件設置的屬性
Django項目中要求包含這些中間件,如果請求中看不到這些屬性, 查看下這些中間件列表 MIDDLEWARE_CLASSES.
-
HttpRequest.session
-
來自
SessionMiddleware中間件:一個可讀寫類似字典對象的當前會話
-
HttpRequest.site
-
來自
CurrentSiteMiddleware中間件: An instance ofSiteorRequestSiteas returned byget_current_site()representing the current site.
-
HttpRequest.user
-
來是
AuthenticationMiddleware中間件: An instance ofAUTH_USER_MODELrepresenting the currently logged-in user. If the user isn’t currently logged in,userwill be set to an instance ofAnonymousUser. You can tell them apart withis_authenticated(), like so:if request.user.is_authenticated(): ... # Do something for logged-in users. else: ... # Do something for anonymous users.
方法
-
HttpRequest.get_host() [source]
-
返回從HTTP_X_FORWARDED_HOST(ifUSE_X_FORWARDED_HOSTis enabled)和HTTP_HOSTheaders獲取的主機名和端口信息像PEP 3333的格式例:
"127.0.0.1:8000"注意:
get_host()獲取失敗,說明后面有很多代理. 一個解決方案是使用中間件來重寫代理頭文件,如下面的例子:
View Codeclass MultipleProxyMiddleware(object): FORWARDED_FOR_FIELDS = [ 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED_HOST', 'HTTP_X_FORWARDED_SERVER', ] def process_request(self, request): """ Rewrites the proxy headers so that only the most recent proxy is used. """ for field in self.FORWARDED_FOR_FIELDS: if field in request.META: if ',' in request.META[field]: parts = request.META[field].split(',') request.META[field] = parts[-1].strip()
該中間就被其他中間件需要get_host()獲取值的中間就依賴– 例如,CommonMiddlewareorCsrfViewMiddleware.
-
HttpRequest.get_port() [source]
-
Django 1.9新的
返回包含HTTP_X_FORWARDED_PORT(ifUSE_X_FORWARDED_PORTis enabled) 和SERVER_PORTMETA的值
-
HttpRequest.get_full_path() [source]
-
返回包含路徑和查詢字符串的路徑
例:
"/music/bands/the_beatles/?print=true"
-
HttpRequest.build_absolute_uri(location) [source]
-
返回一個絕對的url地址
例如:
"https://example.com/music/bands/the_beatles/?print=true"注意:
如果混合了http和https是獲取不准確的,除非全部重定向到https
-
Returns a cookie value for a signed cookie, or raises a
django.core.signing.BadSignatureexception if the signature is no longer valid. If you provide thedefaultargument the exception will be suppressed and that default value will be returned instead.The optional
saltargument can be used to provide extra protection against brute force attacks on your secret key. If supplied, themax_ageargument will be checked against the signed timestamp attached to the cookie value to ensure the cookie is not older thanmax_ageseconds.For example:
View Code>>> request.get_signed_cookie('name') 'Tony' >>> request.get_signed_cookie('name', salt='name-salt') 'Tony' # assuming cookie was set using the same salt >>> request.get_signed_cookie('non-existing-cookie') ... KeyError: 'non-existing-cookie' >>> request.get_signed_cookie('non-existing-cookie', False) False >>> request.get_signed_cookie('cookie-that-was-tampered-with') ... BadSignature: ... >>> request.get_signed_cookie('name', max_age=60) ... SignatureExpired: Signature age 1677.3839159 > 60 seconds >>> request.get_signed_cookie('name', False, max_age=60) False
See cryptographic signing for more information.
-
HttpRequest.is_secure() [source]
-
返回是否是https請求
-
HttpRequest.is_ajax() [source]
-
返回是否是異步請求
-
HttpRequest.read(size=None) [source]
-
HttpRequest.readline() [source]
-
HttpRequest.readlines() [source]
-
HttpRequest.xreadlines() [source]
