The Road to WeChat Mini Program Development (7) Django View

The Road to WeChat Mini Program Development (7) Django View

A view function, or view for short, is a simple Python function that accepts web requests and returns web responses.
The response can be an HTML page, a 404 error page, a redirect page, an XML document, or an image...
No matter what logic the view itself contains, the response must be returned. The code can be written anywhere, as long as it is under the Python directory, usually in the views.py file of the project.
Each view function is responsible for returning an HttpResponse object, which contains the generated response.
There are two important objects in the view layer: request object (request) and response object (HttpResponse).
Request object: HttpRequest object (request object for short)
1. The GET
data type is QueryDict, an object similar to a dictionary, containing all the parameters of HTTP GET.
If you have the same key, put all the values ​​in the corresponding list.
Value format: object. method .
get(): returns a string, if the key corresponds to multiple values, get the last value of the key. Insert picture description here
Insert picture description here
Insert picture description here
2. The POST
data type is QueryDict, a dictionary-like object that contains all the parameters of HTTP POST.
Often used in form forms, the name attribute of the label in the form form corresponds to the key of the parameter, and the value attribute corresponds to the value of the parameter.
Value format: object. method .
get(): returns a string, if the key corresponds to multiple values, get the last value of the key. Insert picture description here
Insert picture description here
3. body
The data type is a binary byte stream, which is the parameter content in the original request body. It is used for POST in HTTP because GET has no request body.
Not commonly used in HTTP, but very useful when processing non-HTTP messages, such as binary images, XML, Json, etc.
Insert picture description here
Insert picture description here
Insert picture description here
4. Path
gets the path part in the URL, the data type is a string.
Insert picture description here
Insert picture description here
5. The method to
obtain the current request, the data type is a string, and the result is uppercase.
Insert picture description here
Insert picture description here
Response object: HttpResponse object
There are three main forms of response object: HttpResponse(), render(), redirect().
HttpResponse(): Returns text, the parameter is a string, and the text content is written in the string. If the parameter contains html tags in the string, it can also be rendered. Insert picture description here
Insert picture description here
Insert picture description here
render(): returns the text, the first parameter is request, the second parameter is a string (page name), the third parameter is a dictionary (optional parameter, parameters passed to the page: the key is the page parameter name, the value Is the view parameter name). Insert picture description here
Insert picture description here
Insert picture description here
redirect(): redirect, jump to a new page. The parameter is a string, and the page path is filled in the string. Generally used to jump to a new page after the form is submitted.

def runoob(request):
    return redirect("/index/")

Render and redirect are encapsulated on the basis of HttpResponse:
render: the bottom layer returns the HttpResponse object
redirect: the bottom layer inherits the HttpResponse object

Guess you like

Origin blog.csdn.net/xulei1132562/article/details/113589435