zoukankan      html  css  js  c++  java
  • Django的请求生命周期

    Django的请求生命周期

    请求生命周期

    请求生命周期是指当用户在浏览器上输入url到用户看到网页的这个时间段内,Django后台所发生的事情。

    1.客户端发送Http请求

    2 .服务器接收,根据请求头中的url在路由关系进行匹配

    3.匹配成功后,执行指定的views函数

    URL---〉函数     ==〉FBV
    
    URL---〉类         ==〉CBV

    4.业务处理(根据个人需求自定)

    操作数据库:

    原生SQL(pymysql)
    
    Django ORM 

     操作模板

     响应

     5.渲染页面返回给客户端

    1.客户端发送Http请求

    在Django中,当我们访问一个的url时,会通过路由匹配进入相应的html网页中。
    这种访问为get请求,请求只有请求头,没有请求体。
    客户端发送Http请求后,Django会解析请求,进行路由分发。

     

    使用get请求

    使用get方式请求服务器的时候,所有数据包含在url中。

    例如:http://127.0.0.1/index/?user=user1?password=123456

    服务端接收数据:

    request.GET.get("user")
    request.GET.get("password")

    使用post请求

    使用post方式请求服务器的时候,所有数据都在请求体中。

    服务端接收数据:

    request.POST.get("user")
    
    request.POST.get(password"")
    

      

     

    2 .服务器接收,根据请求头中的url在路由关系进行匹配

    注意:上下匹配,一旦匹配,就不会继续匹配。

    所以为了避免匹配到上面的url,而没有匹配到下面的url,会在后面加上。

    3.匹配成功后,执行指定的views函数

    一个url对应一个视图函数,这个模式叫做FBV(Function Base Views)
    一个url对应一个类,这个模式叫做CBV(Class Base views)

    总结:一种函数式的,一种面向对象。

    CBV

    路由信息:

    1 from django.contrib import admin
    2 from django.urls import path
    3 from blog import  views    
    4 urlpatterns = [
    5     path('admin/', admin.site.urls),
    6     path('fbv/',views.FBV),
    7     path('cbv/',views.CBV.as_view()),
    8  ]

    视图函数配置:

     1   from django.shortcuts import render,HttpResponse
     2   from django.views import View
     3   
     4 
     5   
     6  class CBV(View):
     7      def get(self,request):
     8          return render(request, "cbv.html")
     9  
    10      def post(self,request):
    11          return HttpResponse("cbv.post")

    cbv.html:

    1 <body>
    2 <form method="post" action="/cbv/">
    3     {% csrf_token %}
    4     <input type="text">
    5     <input type="submit">
    6 </form>
    7 </body>

    第一次访问是通过get的方式:返回cbv.html

    第二次访问时通过表单的方式:返回cbv.post

    启动django,访问:http://127.0.0.1:8080/cbv/

    输入name,提交数据

    CBV请求过程:

    服务端通过路由映射表匹配成功后会自动去找dispatch方法,然后Django会通过dispatch反射的方式找到类中对应的方法并执行

    类中的方法执行完毕之后,会把客户端想要的数据返回给dispatch方法,由dispatch方法把数据返回经客户端

    例子,把上面的例子中的视图函数修改成如下:

    from django.views import View
    
    class CBV(View):
        def dispatch(self, request, *args, **kwargs):
            print("dispatch......")
            res=super(CBV,self).dispatch(request,*args,**kwargs)
            return res
    
        def get(self,request):
            return render(request, "cbv.html")
    
        def post(self,request):
            return HttpResponse("cbv.get")
    View Code

     FBV

    FBV请求过程:

    用户发送url请求,Django会依次遍历路由映射表中的所有记录,一旦路由映射表其中的一条匹配成功了,
    就执行视图函数中对应的函数名,这是fbv的执行流程。
    

      

    视图函数配置:

    1 def FBV(request):
    2     if request.method == "GET":
    3         return HttpResponse("fbv.html")
    4     if request.method == "POST":
    5         return HttpResponse("FBV.post")

    fbv.html

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>Title</title>
     6 </head>
     7 <body>
     8 <body>
     9 <form method="post" action="/fbv/">
    10      {% csrf_token %}
    11     <input type="text">
    12     <input type="submit">
    13 </form>
    14 </body>
    15 </body>
    16 </html>

    Django请求生命周期之响应内容

    http提交数据的方式有"post","get","put","patch","delete","head","options","trace".
    

    from表单支持get与post请求。

    ajax支持全部请求。

    在响应内容中设置请求头与实体

     1  
     2 class CBV(View):
     3      def dispatch(self, request, *args, **kwargs):
     4          print("dispatch......")
     5          res=super(CBV,self).dispatch(request,*args,**kwargs)
     6          print(res)
     7      return res
     8 
     9      def get(self,request):
    10          return render(request, "cbv.html")
    11 
    12      def post(self,request):
    13  
    14          res=HttpResponse("cbv.post")#请求实体
    15        res['head']="head"         #设置请求头
    16          res.set_cookie("k2","v2")    #设置cookies
    17          res.set_cookie("k4","v4")    
    18          print("res:",res)
    19          print("request.cookie:",request.COOKIES)
    20          return res

     图片来源:https://blog.csdn.net/wswhero/article/details/80831874

  • 相关阅读:
    牛客练习赛71 F-红蓝图 (kruskal重构树 + 线段树合并)
    2020杭电多校第一场 Finding a MEX
    Codeforces 235C Cyclical Quest (后缀自动机)
    HDu6583 Typewriter (后缀自动机 + dp)
    2020牛客暑期多校训练营(第八场)A All-Star Game
    HDu4416 Good Article Good sentence (后缀自动机)
    icpc小米 A . Intelligent Warehouse
    计数类dp
    主席树
    博弈论
  • 原文地址:https://www.cnblogs.com/-wenli/p/10426161.html
Copyright © 2011-2022 走看看