1,httprequest Knowledge Summary
Definition:
After the server receives the HTTP protocol request, it creates a HttpRequest object based on the message, which does not need to be created by us, and can be directly used by the server to construct the object. The first parameter of the view must be an HttpRequest object, and the API for the HttpRequest object is defined in the Django.http module. Properties:
Path: A string that represents the full path of the requested page and does not contain a domain name.
method: A string that represents the HTTP method used by the request, and the common values are: ' GET ', ' POST '.
in the browser, give the address to make a request using Get method, such as hyperlink.
Click the form's Submit button in the browser to initiate the request if the form's method is set to post.
encoding: A string that represents the encoding of the data being submitted.
If none is the default setting for using the browser, it is generally utf-8.
This property is writable and can be modified to modify the encoding
used to access the form data Get: A dictionary-like object that contains all the arguments for how a get is requested.
Post: A dictionary-like object that contains all the parameters of the POST request method.
files: A dictionary-like object that contains all the uploaded files.
cookies: A standard Python dictionary that contains all the cookies, keys, and values as strings. Session
: A dictionary-like object that is readable and writable, representing the current session and available only if Django enables session support
Querydict ObjectObjects that are defined in the Django.http.QueryDict HttpRequest object get, post are querydict types are different from Python dictionaries, and objects of the querydict type are used to handle the same key with multiple values
Method Get (): Gets the value based on the key
If a key has more than one value at a time, it gets the last value
Returns a value of None if the key does not exist, you can set the default value for subsequent processing
Dict.get (' key ', default value)
can be abbreviated to
dict[' key ']
Method GetList (): Gets the value from the key, the value is returned as a list, you can get all the values of the specified key
Returns an empty list if the key does not exist [], you can set a default value for subsequent processing
Dict.getlist (' key ', default value)
Get PropertyRequest format: Used at the end of the request address, followed by "key = value" in the format of splicing, multiple key-value pairs with & connection.
Example: The URL is as follows
Http://www.baidu.com/?name=xiaoke&age=26&hobby=python
The request parameters are:
Name=xiaoke&age=26&hobby=python
Parse Request parameters:
The key is ' name ', ' age ', ' hobby ', and the value is ' Xiaoke ', ' 26 ', ' Python '.
In Django, you can use the Get property of the HttpRequest object to get the parameters requested by the Get method.
The Get property returns an object of type querydict, and the key and value are string types.
Keys are determined by the developer as they write the code.
Values are generated based on the data. The Post property uses the form form request, the method is post to initiate a post-mode request, the Post property of the HttpRequest object is used to receive the parameter, and the Post attribute returns an object of type Querydict.
Q: How do form form forms submit parameters?
A: The value of the control's Name property is a key, and the value for the Value property is the values that make up the key-value pair submission.
If the control does not have a Name property, it is not committed.
For a CheckBox control, the value of the Name property is the same as a group, and the selected item is committed with a one-key multi-value case.
The key is the value of the control's Name property, which is written by the developer.
Values are filled in or selected by the user. 2,httpresponse Object definition
The view must return a HttpResponse object or child object after it receives the request and processes it. The API for the HttpResponse object is defined in the Django.http module. HttpRequest objects are created by Django, and HttpResponse objects are created by developers. Property Content: Indicates what is returned. CharSet: Represents the coded character set used by response, which defaults to Utf-8. Status_code: Returns the HTTP response status code. Content-type: Specifies the MIME type of the returned data, which defaults to ' text/html '. method Init: The initialization of the returned content is completed after the HttpResponse object is created. Set_cookie: Set cookie information. Write: Writes data to the response body.
A cookie is a piece of plain text information that a Web site stores in a browser in a key-value pair format for user tracking.
–max_age is an integer that indicates that it expires after a specified number of seconds.
–expires is a datetime or Timedelta object, and the session expires at this specified date/time.
–max_age and expires two choose one.
If you do not specify an expiration time, the cookie expires when you close the browser.
Set_cookie (Key, value= ", Max_age=none, Expires=none)
Delete_cookie (key): Deletes the cookie for the specified key and does not occur if key does not exist. sub-class Jsonresponse
Returns JSON-formatted data when using JavaScript to initiate AJAX requests in the browser. The class Jsonresponse inherits from the HttpResponse object, which is defined in the Django.http module and receives the dictionary as a parameter when the object is created. The Content-type of the Jsonresponse object is ' Application/json '. sub-class Httpresponseredirect
When a logical processing is done, it is not necessary to render the data to the client, but to go back to the other pages, such as the success of the addition, the success of the modification, the data list is displayed after successful deletion, and the list view of the data has been developed, there is no need to rewrite the code of the list, but to go to this view At this point, you need to simulate the effect of a user request, from one view to another, called a redirect. Django provides the Httpresponseredirect object implementation redirection feature, which inherits from HttpResponse, is defined in the Django.http module, and returns a status code of 302.