Django HTTP request detailed

Source: Internet
Author: User
Tags auth documentation http request server port

The first parameter of each of Django's view functions is request, have you ever thought about what is in the request?

Django uses the request and response objects to pass states between systems.

When a page is asked, Django creates a HttpRequest object that contains the request metadata. Django then switches into the appropriate view, passing the HttpRequest as the first argument to the view function. Each view is responsible for returning a HttpResponse object.


The properties of the HttpRequest instance contain most of the important information about this request (see table H-1). All attributes except the session should be considered read-only.


Property Description
Path A string that represents the full address of the submit Request page, excluding the domain name, such as "/music/bands/the_beatles/".
Method

Represents the HTTP method used for submitting a request. It's always capitalized. For example:

if Request.method = = ' Get ':
    do_something ()
elif Request.method = = ' POST ':
    do_something_else ()
Get A class Dictionary object that contains information about all of the HTTP GET parameters. See querydict documentation.
POST

A class Dictionary object that contains information about all the post parameters of HTTP. See querydict documentation.

A request submitted by post may contain an empty post dictionary, that is, a form submitted through the Post method may not contain data. Therefore, you should not use the IF request. Post to determine the use of the Post method, instead of using the If Request.method = "POST" (see the methods entry in the table).

Note: POST does not contain file upload information. See FILES.

REQUEST

For convenience, this is a class Dictionary object that first searches for a POST and then searches for a get. The inspiration comes from the $_reqeust of PHP.

For example, if get = {"name": "John"}, POST = {"Age": ' request['}, "name"] will be "John", request["age" will be "34".

It is highly recommended to use get and POST instead of REQUEST. This is for forward compatibility and a clearer representation.

COOKIES A standard Python dictionary that contains all cookies. Both the key and the value are strings. More information for use with cookies is shown in chapter 12th.
FILES

A class Dictionary object that contains all uploaded files. The key for FILES comes from name in <input type= "file" Name= ""/>. The files value is a standard Python dictionary that contains the following three keys: FileName: String representing the file name of the uploaded file. Content-type: The content type of the uploaded file. Content: Upload the original contents of the file.

Note that FILES only contain data when the requested method is POST, and the submitted <form> contains enctype= "Multipart/form-data". Otherwise, the FILES are just an empty class Dictionary object.

META

A standard Python dictionary that contains all valid HTTP header information. Valid header information is associated with the client and server. Here are a few examples: Content_length content_type query_string : The original request string that was not resolved. REMOTE_ADDR&NBSP: Client IP address. REMOTE_HOST&NBSP: Client host name. SERVER_NAME&NBSP: Server host name. SERVER_PORT&NBSP: Server port number. The

Any HTTP header information that is valid in  META  is a key with  HTTP_  prefix, for example: Http_accept_encoding http_accept_language http _HOST&NBSP: The  Host  header information sent by the client. HTTP_REFERER&NBSP: The page to be pointed, if present. HTTP_USER_AGENT&NBSP: The user-agent string of the client. The value of the http_x_bender : x-bender  header information, if it is already set.

User

A Django.contrib.auth.models.User object represents the currently logged-on user. If the current user is not logged in, user is set to an instance of Django.contrib.auth.models.AnonymousUser. They can be distinguished from is_authenticated ():

If request.user.is_authenticated ():
    # do something for logged-in users.
else:
    # do something for anonymous users.

User is only valid when Django activates Authenticationmiddleware.

For complete details on certifications and users, see Chapter 12th.

Session A readable and writable class Dictionary object that represents the current session. Valid only if Django has activated session support. See http://djangobook.py3k.cn/chapter12/
Raw_post_data The original data for the post. Used for complex processing of data.

The Request object also contains some useful methods, as shown in table H-2.

Table H-2. The method of HttpRequest
Method Description
__GETITEM__ (Key)

Request the Get/post value of the given key, first find the post, then the get. If the key does not exist, an exception keyerror is thrown.

This method enables the user to access a HttpRequest instance in the form of an Access dictionary.

For example, request["foo"] and check the request first. post["foo"] check the request again. get["foo"].

Has_key () Returns TRUE or False to identify the request. Get or request. Whether the POST contains the given key.
Get_full_path () Returns path, if the request string is valid, attached to the following. For example, "/music/bands/the_beatles/?print=true".
Is_secure () Returns True if the request is safe. In other words, the request is submitted in HTTPS form.



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.