Directory
- I. Understanding of the View function
- Second, the positioning of the view function
- Third, the Request object HttpRequest
- Iv. Response Object HttpResponse
I. Understanding of the View function
The function of the view function is that the url view function will act as a bridge to the layer and layer for the specified execution of the business logic, and the model template most important logic is to manipulate the database and prepare the context before the template is rendered.
Module of the View system:
- Request Object
- Business logic
2.1 Connection model Layer
2.2 Connection template Layer
- Response Object
Second, the positioning of the view function
All frameworks that implement wsgi a protocol web must implement a function in the form of an interface that completes the response to the application http request http , web and the demarcation of the application and web server begins with this function.
applicationFunctions are web defined in the application, and wsgi are called indefinitely on the server ( web server), in which the django application function is based on the routing system url to find the corresponding view , and then executes view The view function handles the business logic.
# 伪代码def application(environ, start_response): # 此函数在web应用程序上被定义,在web服务器上被无限循环调用 view = route(environ.url) # 路由系统 response = view(environ, *args, **kw) # 视图系统 return response
Django View feature positioning diagram
The essential function of a view function is to receive a reqeust request, handle it correctly, and then return a reponse response.
To facilitate the processing of requests and responses in the view function, the reqeust request message is encapsulated in the web framework as a HttpRequest request object, and the reponse response message is encapsulated as a HttpResponse response object.
So in the view function, in addition to the processing logic, there are two important objects, namely the request object and the response object.
Third, the Request object
HttpRequest
The request object is the encapsulation of the http request message, django automatically executing the encapsulation and passing in the first parameter of the view function, we need to call in the view function.
There are many data in the request object that need to be processed further, and the commonly used interfaces are as follows:
常用接口:HttpRequest.method 请求方法HttpRequest.GET 对应GET请求类型的数据字典HttpRequest.POST 对应POST请求类型的数据字典HttpRequest.path 请求的路径HttpRequest.get_full_path() 请求的路径+get数据HttpRequest.is_ajax() 判断是否为ajax形式的请求
Iv. Response Objects
HttpResponse
The response object is the encapsulation of the http response message, the response object needs us to create in the view function, and at the end of the view function return to the upper function is the bottom control loop. The response object means the web response information returned by the service program after it is processed by the business logic.
There are two main ways to return a response object in a view function:
Method 1: The string literal is the interface: the return HttpResponse(‘xxx‘) directly returned string will be used as html the content of the page and will be executed by the browser, that is, you can return a
Mode 2: Use a html file or template file as an interface: return render(request, ‘index.html‘, {xxx}) You can return the final form of the html string directly. renderthe rendering engine is called by the function to perform rendering operations on the template file.
The template file contains the template syntax, which is considered a html placeholder in the file for subsequent padding of dynamic data, and the render last action is performed inside the function return HttpResponse .
Note: In order to improve security, functions are automatically escaped to special characters when they render {xxx} contain executable html data when processing the render context, primarily to prevent insertion of similar link or script The security issues that these browsers will execute in the html code to the client. The render function flow is to render http the response body string in the response message, and then return an HttpResponse object, which is more than the rendering process, in relation to mode 1.
Django Framework--View system