FBV和CBV

fbv就是一个 url对应一个函数

cbv就是一个url对应一个类

cbv

  • cbv定义类的时候必须要继承view
  • 这个类里面可以写好多函数方法,但是本质上都是通过dispatch这个函数反射执行
  • 在写url的时候必须要加as view

定义cbv

给cbv添加装饰器的三个位置

csrf的添加

必须要加到dispatch里面

  1. FBV和CBV
    function base view, URL对应函数
    class base view, URL对应类

    PS:
     form表单提交:    GET,POST   ***
     
     Ajax提交数据:    GET,POST
                       ['get'获取, 'post'创建, 'put'更新, 'patch'局部更新, 'delete'删除, 'head', 'options', 'trace'] -> restful规范
    

    a. 基本使用

    b. 基于dispatch和继承实现用户登录代码

    c. 装饰器

     get,post方法上
    
                 class LoginView(View):
    
                     
    
                     def dispatch(self, request, *args, **kwargs):
    
                         return super(LoginView,self).dispatch(request, *args, **kwargs)
    
    
    
                     def get(self,request):
    
                         return render(request,'login.html')
    
    
    
                     @method_decorator(test)
    
                     def post(self,request):
    

request.GET

request.POST # 请求头中的:content-type

request.body

                        user = request.POST.get('user')

                        pwd = request.POST.get('pwd')

                        if user == 'alex' and pwd == "alex3714":

生成随机字符串

写浏览器cookie: session_id: 随机字符串

写到服务端session:

{

"随机字符串": {'user_info':'alex}

}

                            request.session['user_info'] = "alex"

                            return redirect('/index.html')

                        return render(request, 'login.html')

    dispatch方法上

        class LoginView(View):

            @method_decorator(test)

            def dispatch(self, request, *args, **kwargs):

                return super(LoginView,self).dispatch(request, *args, **kwargs)



            def get(self,request):

                return render(request,'login.html')

            def post(self,request):

request.GET

request.POST # 请求头中的:content-type

request.body

                user = request.POST.get('user')

                pwd = request.POST.get('pwd')

                if user == 'alex' and pwd == "alex3714":

生成随机字符串

写浏览器cookie: session_id: 随机字符串

写到服务端session:

{

"随机字符串": {'user_info':'alex}

}

                    request.session['user_info'] = "alex"

                    return redirect('/index.html')

                return render(request, 'login.html')

    类上

        @method_decorator(test,name='get')

        class LoginView(View):

            

            def dispatch(self, request, *args, **kwargs):

                return super(LoginView,self).dispatch(request, *args, **kwargs)



            def get(self,request):

                return render(request,'login.html')

                def post(self,request):
                    # request.GET
                    # request.POST # 请求头中的:content-type
                    # request.body
                    user = request.POST.get('user')
                    pwd = request.POST.get('pwd')
                    if user == 'alex' and pwd == "alex3714":
                        # 生成随机字符串
                        # 写浏览器cookie: session_id: 随机字符串
                        # 写到服务端session:
                        # {
                        #     "随机字符串": {'user_info':'alex}
                        # }
                        request.session['user_info'] = "alex"
                        return redirect('/index.html')
                    return render(request, 'login.html')
                    
        特殊:CSRF Token只能加到dispatch

                from django.views.decorators.csrf import csrf_exempt,csrf_protect
                class LoginView(View):
                    @method_decorator(csrf_exempt)
                    def dispatch(self, request, *args, **kwargs):
                        return super(LoginView,self).dispatch(request, *args, **kwargs)

                    def get(self,request):
                        return render(request,'login.html')

                    def post(self,request):
                        # request.GET
                        # request.POST # 请求头中的:content-type
                        # request.body
                        user = request.POST.get('user')
                        pwd = request.POST.get('pwd')
                        if user == 'alex' and pwd == "alex3714":
                            # 生成随机字符串
                            # 写浏览器cookie: session_id: 随机字符串
                            # 写到服务端session:
                            # {
                            #     "随机字符串": {'user_info':'alex}
                            # }
                            request.session['user_info'] = "alex"
                            return redirect('/index.html')
                        return render(request, 'login.html')

你可能感兴趣的:(FBV和CBV)