Django's HTPP request Wsgirequest

Source: Internet
Author: User

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:

WSGIRequestMost 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:

  1. 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/ .
  2. method: Represents the method of the current request http . Like,GET、POST、delete或者是put等方法
  3. 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.
  4. POST: is also an django.http.request.QueryDict object. This property contains all the parameters that are uploaded in a POST way.
  5. FILES: is also an django.http.request.QueryDict object. All uploaded files are included in this property.
  6. COOKIES: A standard Python dictionary that contains all of the cookie key-value pairs that are string types.
  7. session: A dictionary-like object. Used to operate the server session .
  8. 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 () .

  9. META: All the information that the stored client sends up header , here are the common header information:

    1.   CONTENT_LENGTH: The length of the body of the request (is a string).

    2.   CONTENT_TYPE: The MIME type of the body of the request.
    3.   HTTP_ACCEPT: Responds to Content-type that can be received.
    4.   HTTP_ACCEPT_ENCODING: A response to a received encoding that tells the server that the client can handle the encoding and relative priority.
    5.   HTTP_ACCEPT_LANGUAGE: Responds to a language that can be received.
    6.   HTTP_HOST: The host value sent by the client.
    7.   HTTP_REFERER: The URL of the previous page in the Access page.
    8.   QUERY_STRING: A query string in the form of a single string (in unresolved form).
    9. 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)
    10.   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‘]
    11.   REMOTE_HOST: The host name of the client.
    12.   REQUEST_METHOD: Request method. A string similar to GET or POST .
    13.   SERVER_NAME: Server domain name.
    14.   SERVER_PORT: Server port number, is a string type.
Common methods for Wsgirequest objects:
    1. is_secure(): Is the adoption https agreement.
    2. 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 .
    3. 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 .
    4. 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 .
    5. 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.

    1. getMethod: Used to get the specified key value, and if not key , it will be returned None .
    2. getlistMethod: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.