python---django要求-回應的生命週期(FBV和CBV含義)

來源:互聯網
上載者:User

標籤:業務   inf   http請求   ike   ISE   __name__   方式   roc   基本   

Django請求的生命週期是指:當使用者在訪問該url路徑是,在伺服器Django後台都發生了什麼。

用戶端發送Http請求給服務端,Http請求是一堆字串,其內容是:

訪問:http://crm.oldboy.com:8080/login.html,用戶端發送Http請求

1.路由映射,匹配路由(從上到下,匹配到就停止),對應相應views中的業務函數

url(r‘^login.html‘, views.login),

2.匹配成功後,執行views下的對應函數:(FBV)

def login(req):    print(‘req.body‘,req.body)    print("GET",req.GET)    message=‘‘    if req.method == "POST":        print(req.body)        print(req.POST)        user = req.POST.get("username")        pwd = req.POST.get("password")        count = models.Administrator.objects.filter(username=user,password=pwd).count()        if count:            red = redirect("/index.html")            timeout = datetime.datetime.now()+datetime.timedelta(seconds=3)            red.set_cookie(‘username‘,user,expires=timeout)            return red        else:            message = "使用者名稱或密碼錯誤"    return render(req,"login.html",{‘msg‘:message})
View Code
URL -->  函數  ====>  FBV(Function-based views)    基於函數的視圖
URL -->  類    ====> CBV (Class-based views)    基於類的視圖

FBV:在Django中使用較多,在其他架構中多使用CBV,例如tornado,還有PHP的多種架構等

Django中CBV使用:

首先需要設定views中的類:

from django.views import Viewclass CBV(View):
   #根據要求標頭中的request method進行自動執行get和post def get(self,request): return render(request,"cbv_login.html") def post(self,request): return HttpResponse("<h1>cbv_post</h1>")

然後修改urls檔案路由:

urlpatterns = [    url(r"cbv",views.CBV.as_view())]

模板檔案:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><form action="/cbv" method="post">    {% csrf_token %}    <div>        <label for="user">使用者名稱:</label>        <input type="text" id="user" name="username"/>    </div>    <div>        <label for="pwd">密碼:</label>        <input type="password" id="pwd" name="password"/>    </div>    <div>        <label></label>        <input type="submit" value="登入">        <label>{{ msg }}</label>    </div></form></body></html>
cbv_login.html

使用url訪問預設是get方式,顯示cbv_login.html頁面,提交頁面,進入post頁面,顯示cbv_post資料

get還是post,是由於要求標頭中的Request Method:擷取,從而找到對應方法。使用反射尋找,來執行對應方法。

1.由Request URL請求去擷取路徑,與urls進行匹配,找到對應的類2.由請求體得到:Request Method:GET3.獲得類中方法   方法名  =  getattr(對象,"GET")   方法名()    #執行對應函數

源碼查看:

@classonlymethod    def as_view(cls, **initkwargs):        """        Main entry point for a request-response process.要求-回應的主進入點,在url解析時調用        """     for key in initkwargs:     #cls.http_method_names:     #[u‘get‘, u‘post‘, u‘put‘, u‘patch‘, u‘delete‘, u‘head‘, u‘options‘, u‘trace‘]            if key in cls.http_method_names:                raise TypeError("You tried to pass in the %s method name as a "                                "keyword argument to %s(). Don‘t do that."                                % (key, cls.__name__))            if not hasattr(cls, key):                raise TypeError("%s() received an invalid keyword %r. as_view "                                "only accepts arguments that are already "                                "attributes of the class." % (cls.__name__, key))        #print(cls)  #<class ‘app1.views.CBV‘>        def view(request, *args, **kwargs):            self = cls(**initkwargs)  #執行個體化CBV對象            if hasattr(self, ‘get‘) and not hasattr(self, ‘head‘):                self.head = self.get            self.request = request            #print(request)    <WSGIRequest: GET ‘/cbv‘>            #print(request.method)  GET            self.args = args            self.kwargs = kwargs            return self.dispatch(request, *args, **kwargs)#調用dispatch方法,將<WSGIRequest: GET ‘/cbv‘>傳入        view.view_class = cls        view.view_initkwargs = initkwargs        # take name and docstring from class        update_wrapper(view, cls, updated=())        # and possible attributes set by decorators        # like csrf_exempt from dispatch        update_wrapper(view, cls.dispatch, assigned=())        return view    def dispatch(self, request, *args, **kwargs):        # Try to dispatch to the right method; if a method doesn‘t exist,        # defer to the error handler. Also defer to the error handler if the        # request method isn‘t on the approved list.        if request.method.lower() in self.http_method_names:            handler = getattr(self, request.method.lower(), self.http_method_not_allowed) #去調用對應的函數        else:            handler = self.http_method_not_allowed        return handler(request, *args, **kwargs)

推薦:介紹——基於類的視圖(class-based view)

3.業務處理

-----根據個人需求自訂

-----對於架構:基本操作是操作資料庫

  ---pymysql   (原生)

  ---SQLAlchemy

  ---Django中orm

-----響應內容:返回給使用者的結果:回應標頭和響應體

我們寫的HTTPResponse是寫在響應體中

回應標頭的定製:

    def post(self,request):        ret =  HttpResponse("<h1>post</h1>")
     #下面為佈建要求頭 ret[‘h1‘] =‘v1‘ ret.set_cookie(‘c1‘,‘v1‘) ret.set_cookie(‘c2‘,‘v2‘) ‘‘‘ 回應標頭:h1=v1 cookies:c1=v1;c2=v2 響應體:<h1>post</h1> 要求標頭資訊: Content-Length:13 Content-Type:text/html; charset=utf-8 Date:Wed, 28 Mar 2018 13:54:53 GMT h1:v1 Server:WSGIServer/0.1 Python/2.7.10 Set-Cookie:c2=v2; Path=/ Set-Cookie:c1=v1; Path=/ X-Frame-Options:SAMEORIGIN ‘‘‘ return ret

 

python---django要求-回應的生命週期(FBV和CBV含義)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.