请求生命周期

发布时间 2023-12-06 17:34:56作者: 木屐呀

-请求响应Http

  • 1.发送Http请求
  • 2.服务器接收,根据请求头中的url在路由关系表中进行匹配(从上到下)
  • 3.匹配成功后,执行指定的views函数
  •   URL -> 函数 ==> FBV FUNCTION BASE VIEW 表示url对应视图函数的关系
  •   URL -> 类 ==> CBV
  • 4.业务处理
  • - 根据个人需求
  • - 操作数据库
  • - 原生SQL
  • - Django ORM

==> 返回给用户的结果 <==
  - 响应内容
  - 响应头
  - 响应体

 1 from django.conf.urls import url
 2 from django.contrib import admin
 3 from APP01 import views
 4 
 5 urlpatterns = [
 6         url(r'^admin/', admin.site.urls),
 7         url(r'^inx$', views.inx),
 8         url(r'^inxd$', views.inxd),
 9         url(r'^fbv$', views.fbv),
10         url(r'^cbv$', views.CBV.as_view()),  #as_view 固定写法
11 ]
 1 from django.shortcuts import render,HttpResponse
 2 
 3 # Create your views here.
 4 
 5 
 6 def inx(request):
 7     return HttpResponse("ok")
 8 
 9 def inxd(request):
10     return HttpResponse("ok")
11 
12 def fbv(request):
13     if request.method == "GET":
14         return HttpResponse("FBV.GET")
15     elif request.method == "POST":
16         return HttpResponse("FBV.POST")
17 
18 from django.views import View
19 
20 class CBV(View):
21     def dispatch(self, request, *args, **kwargs):
22         # 可以进行自定制 ,增加一些功能,例如判断登陆
23         print("dispatch...")
24         result = super(CBV,self).dispatch(self, request, *args, **kwargs)
25         return result
26     # django CBV相关的一些函数
27     # http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
28     def get(self,request):
29         #return render(request, 'cbv.html')
30         return HttpResponse("CBV.GET")
31 
32     def post(self,request):
33         # return HttpResponse("CBV.POST")
34         ret = HttpResponse("CBV.POST")
35         ret['h1'] = 'v1' #往响应头加内容
36         ret['h2'] = 'v2'
37         ret.set_cookie('k1','v1') #设置cookie内容
38         ret.set_cookie('k2', 'v2')
39         """
40         头:
41             h1 = v1
42             h2 = v2
43             cookies:k1 = v1;k2 = v2
44         体:
45             CBV.POST
46         """
47         return ret
1     def dispatch(self, request, *args, **kwargs):
2         # Try to dispatch to the right method; if a method doesn't exist,
3         # defer to the error handler. Also defer to the error handler if the
4         # request method isn't on the approved list.
5         if request.method.lower() in self.http_method_names:
6             handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
7         else:
8             handler = self.http_method_not_allowed
9         return handler(request, *args, **kwargs)
dispatch源码

# CBV 补充
方法名 = getattr(对象,“POST”) -> 获得方法也就是函数
方法名() ->执行

请求和响应

 1 ①请求字符串
 2         请求头                                              
 3             Request URL:https://dig.chouti.com/login                  request.GET 获取网址上数据
 4             Request Method:POST
 5             :authority:dig.chouti.com
 6             :method:POST
 7             :path:/login
 8             :scheme:https
 9             accept:*/*
10             accept-encoding:gzip, deflate, br
11             accept-language:zh-CN,zh;q=0.9
12             content-length:43
13             content-type:application/x-www-form-urlencoded; charset=UTF-8
14             cookie:gpid=8156deb10f9e496d8b49416e82bfba84; gpsd=0c47e59f4c63637e6318391f89040aa3; JSESSIONID=aaa-wYdnGlAxR49Fre8zw
15             origin:https://dig.chouti.com
16             referer:https://dig.chouti.com/
17             user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36
18             x-requested-with:XMLHttpRequest
19         分隔(分隔头和体作用):
20         \r\n\r\n
21         请求体                            request.body 字符串==》》   字典 request.POST  获取表单数据
22             phone:86242424
23             password:43434343
24             oneMonth:1
25     
26     响应字符串
27         响应头
28             cache-control:no-cache
29             content-length:72
30             content-type:text/plain; charset=utf-8
31             date:Tue, 16 Oct 2018 14:19:51 GMT
32             eagleid:7b06074415396995911764020e
33             server:Tengine
34             status:200
35             timing-allow-origin:*
36             vary:Accept-Encoding
37             via:cache5.l2cm12[21,0], cache4.cn633[36,0]
38         分隔(分隔头和体作用):
39         \r\n\r\n
40         响应体
41             。。。html。。。。

补充:

1 #正则表达式结尾部分加上$,可以防止仅仅匹配部分的情况,导致匹配不了完整链接            
2 urlpatterns = [
3     url(r'^admin/', admin.site.urls),
4     url(r'^inx$', views.inx),
5     url(r'^inxd$', views.inxd)
6 ]

总结:

源码:找到扩展点很重要