Wsgirequest Object
After the HTTP request is received, Django creates an object based on the parameters and message information that the HTTP request carries, and is passed to the WSGIRequest
view function as the first parameter of the view function. This parameter is the first parameter of the Django view function and is usually written as a request. On this object we can find all the information that the client uploads. The full path to this object is django.core.handlers.wsgi.WSGIRequest
.
The URL of the HTTP request is detailed:
Before we understand the properties and methods of the Wsgirequest object, let's look at the composition of the URL, usually the complete composition of the URL as follows, [] is optional:
Protocol://hostname[:p ort]/path/[;p Arameters][?query] #fragment
- protocol: network protocols, commonly used protocols are HTTP/HTTPS/FTP, etc.
- hostname: host address, can be a domain name, can also be an IP address
- Port : ports HTTP protocol default port is: 80 port, in the browser default will hide does not display
- Path : route network resource specified path in the server
- parameter: parameter if you want to pass in a parameter to the server, in this section enter
- Query : search string If you need to query the content from the server, edit it here
- fragment: fragment pages may be divided into different fragments, if you want to access the page directly to the specified location, you can set in this section
Wsgirequest Object Common Properties:
WSGIRequest
Most of the properties on an object are read-only. Because these properties are uploaded from the client and there is no need to make any modifications, when used in the Django view, the first parameter parameter of the view function is the Wsgirequest object. Some of the commonly used properties are explained below:
path
: The full "path" of the resource on the server, but not the domain name and parameters, which is also the content of path in the URL. For example http://www.baidu.com/xxx/yyy/
, so it path
is /xxx/yyy/
.
method
: Represents the method of the current request http
. Like,GET、
POST、delete或者是put等方法
GET
: An django.http.request.QueryDict
object. Operate like a dictionary. This property contains all the parameters that are ?xxx=xxx
uploaded in the way.
POST
: is also an django.http.request.QueryDict
object. This property contains all the parameters that are uploaded in a POST
way.
FILES
: is also an django.http.request.QueryDict
object. All uploaded files are included in this property.
COOKIES
: A standard Python dictionary that contains all of the cookie
key-value pairs that are string types.
session
: A dictionary-like object. Used to operate the server session
.
User: user is only available when Django enables Authenticationmiddleware middleware. Its value is an object of the class defined by the setting.py inside the auth_user_model field, representing the currently logged-on user. If the user is not currently logged in, users will be set to an instance of Django.contrib.auth.models.AnonymousUser . You can differentiate them by is_authenticated () .
META
: All the information that the stored client sends up header
, here are the common header information:
CONTENT_LENGTH
: The length of the body of the request (is a string).
CONTENT_TYPE
: The MIME type of the body of the request.
HTTP_ACCEPT
: Responds to Content-type that can be received.
HTTP_ACCEPT_ENCODING
: A response to a received encoding that tells the server that the client can handle the encoding and relative priority.
HTTP_ACCEPT_LANGUAGE
: Responds to a language that can be received.
HTTP_HOST
: The host value sent by the client.
HTTP_REFERER
: The URL of the previous page in the Access page.
QUERY_STRING
: A query string in the form of a single string (in unresolved form).
- TE: Sets the encoding format of the transport entity, indicating the type of transfer-encoding that the requesting initiator is willing to receive (encoding format during transmission, between proxy servers)
REMOTE_ADDR
: The IP address of the client. If the server uses a nginx
reverse proxy or load balancer, then this value is returned 127.0.0.1
, which can be used HTTP_X_FORWARDED_FOR
to obtain, so ip
the code snippet to get the address is as follows: if request.META.has_key(‘HTTP_X_FORWARDED_FOR‘): ip = request.META[‘HTTP_X_FORWARDED_FOR‘] else: ip = request.META[‘REMOTE_ADDR‘]
REMOTE_HOST
: The host name of the client.
REQUEST_METHOD
: Request method. A string similar to GET
or POST
.
SERVER_NAME
: Server domain name.
SERVER_PORT
: Server port number, is a string type.
Common methods for Wsgirequest objects:
is_secure()
: Is the adoption https
agreement.
is_ajax()
: Whether ajax
to use the sent request. The principle is to judge whether there is a request in the header X-Requested-With:XMLHttpRequest
.
get_host()
: The domain name of the server. If there is a port number at the time of the visit, the port number is added, and the URL is hostname+port. For example www.baidu.com:9000
.
get_full_path()
: Returns the full path. If you have a query string, you also add a query string that is the path in the URL and all that follows it. For example /music/bands/?print=True
.
get_raw_uri()
: Gets the complete request url
.
Querydict object:
We usually use request.GET、
request.POST和request.FILES
QueryDict
objects, this object inherits from dict
, so the usage is very similar dict
. There are more get
methods and getlist
methods used.
get
Method: Used to get the specified key
value, and if not key
, it will be returned None
.
getlist
Method: If the browser uploads the key
corresponding values have multiple, if you use Get value, then you can only take out the last one value, if you want to fetch all the values, then you need to getlist this method to obtain.
Django's HTPP request wsgirequest