Specific Use of Django View Function

  • 2021-10-13 08:03:14
  • OfStack

View is the V layer in MTV design pattern, which is the key layer to realize business logic. It can be used to connect M layer with T layer and play a link-like role. It is also introduced in "The Difference between Django, MTV and MVC", so its importance can be imagined.

1. The first view function

In Django, the view function is an Python function or class, and developers mainly write view functions to realize business logic. The view function first accepts the request from the browser or client, and finally returns a response. The response returned by the view function can be an HTML file or a 303 redirection in the HTTP protocol. Next, write a simple view function:


from django.http import HttpResponse
def Hello_my_django(request):
 return HttpResponse('<html><body>Hello my Django</body></html>')

The following is an analysis of the above three lines of code:

1) HttpResponse view response type
Importing HttpResponse from the django. http module, we know from its simple name that it is a response type of view.

Common ways to extend on HttpResponse objects:

-Page rendering: render (recommended), render_to_response, -Page jump: redirect -locals: You can directly pass all variables in the corresponding view function to the template

2) View function parameter request
We define a function named "Hello_my_django". Django stipulates that the view function has at least one parameter, the first parameter must be request, and request is an object of HttpRequest request type, which carries the request information of the browser, so the first parameter of the view function must be request.

When a page is requested, Django creates a request with an HttpRequest object containing the original data. Django then loads the appropriate view with HttpRequest as the first parameter of the view function. Each view is responsible for returning 1 HttpResponse target.


path:  请求页面的全路径,不包括域名
method:  请求中使用的HTTP方法的字符串表示。全大写表示。例如
     if req.method=="GET":
        do_something()
     elseif req.method=="POST":
        do_something_else()
GET:   包含所有HTTP GET参数的类字典对象
POST:  包含所有HTTP POST参数的类字典对象
    服务器收到空的POST请求的情况也是可能发生的,也就是说,表单form通过
    HTTP POST方法提交请求,但是表单中可能没有数据,因此不能使用
    if req.POST来判断是否使用了HTTP POST 方法;应该使用 if req.method=="POST"
COOKIES:  包含所有cookies的标准Python字典对象;keys和values都是字符串。
FILES:  包含所有上传文件的类字典对象;FILES中的每1个Key都是<input type="file" name="" />标签中 
   name属性的值,FILES中的每1个value同时也是1个标准的python字典对象,包含下面3个Keys:
   filename:  上传文件名,用字符串表示
   content_type: 上传文件的Content Type
   content:  上传文件的原始内容
user:  是1个django.contrib.auth.models.User对象,代表当前登陆的用户。如果访问用户当前
    没有登陆,user将被初始化为django.contrib.auth.models.AnonymousUser的实例。你
    可以通过user的is_authenticated()方法来辨别用户是否登陆:
    if req.user.is_authenticated();只有激活Django中的AuthenticationMiddleware
    时该属性才可用
session: 唯1可读写的属性,代表当前会话的字典对象;自己有激活Django中的session支持时该属性才可用。
META:  1个标准的Python字典包含所有可用的HTTP头。可用标题取决于客户端和服务器,但这里是1些例子:
   CONTENT_LENGTH  � 请求体的长度(1个字符串)。
   CONTENT_TYPE   � 请求体的类型。
   HTTP_ACCEPT   - 为响应�可以接受的内容类型。
   HTTP_ACCEPT_ENCODING � 接受编码的响应
   HTTP_ACCEPT_LANGUAGE � 接受语言的反应
   HTTP_HOST   � 客户端发送的HTTP主机头。
   HTTP_REFERER   � 参考页面
   HTTP_USER_AGENT  � 客户端的用户代理字符串。
   QUERY_STRING   � 查询字符串,作为1个单1的(分析的)字符串。
   REMOTE_ADDR   � 客户端的IP地址
   REMOTE_HOST   � 客户端的主机名
   REMOTE_USER   � 用户通过Web服务器的身份验证。
   REQUEST_METHOD  � 字符串,如"GET"或"POST"
   SERVER_NAME   � 服务器的主机名
   SERVER_PORT   � 服务器的端口(1个字符串)。

3) return view response
The view function returns the response content, which we wrote in the HTML tag and returned to the browser as an HttpResponse object.

2. View function execution

Although the code of the view function above is only a few lines, it has fully reflected the implementation process of the view layer. After receiving the request, Django first creates an HttpRequset object with request information, and passes the request object of HttpRequest as the first parameter to the view function. After receiving the parameter, the view continues to execute downward, then selects and loads the corresponding view, and finally returns the HttpResponse object to the browser.

Through the introduction in this section, we should understand the function of View view function and the definition process of view function.


Related articles: