四、视图层

一、HttpResponse

1. 视图函数必须有一个返回值,且返回值数据类型必须是HttpResponse对象

2. 三个HttpResponse对象

    a. HttpResponse
    b. render
    c. redirect

二、JsonResponse

1. 通常前后端数据交互采用json格式字符串(字典)


2. JsonResponse向前端传输字典

    from django.http import JsonResponse
    
    
    def index(request):
    
        user_dict = {'name': 'byx', 'msg': '想放假'}
    
        return JsonResponse(user_dict, json_dumps_params={'ensure_ascii': False})


3. JsonResponse向前端传输其他数据类型

    from django.http import JsonResponse
    
    
    def index(request):
    
        l = [1, 2, 3]
    
        return JsonResponse(l, safe=False)

三、FBV与CBV

1. 定义

    FBV:基于函数的视图
    CBV:基于类的视图
    
2. CBV基本写法

    a. 视图层
    
        from django.shortcuts import render, HttpResponse, redirect
        from django.views import View
        
        
        class Mylogin(View):
        
            def get(self, request):
                print('我是MyLogin里面的get方法')
                return render(request, 'login.html')
        
            def post(self, request):
                print('我是MyLogin里面的post方法')
                return HttpResponse('post')
    
    b. 路由层

        urlpatterns = [
            url(r'^admin/', admin.site.urls),
            # FBV路由写法
            url(r'^index/', views.index),
            # CBV路由写法
            url(r'^login/', views.Mylogin.as_view()),
        ]

3. CBV特点及源码内部原理

    a. 特点
        
        自动根据不同请求方式执行不同方法
    
    b. 源码内部原理
    
        根据接收的request参数,用反射获取到对应的方法。
    
        # 如果想在视图函数执行之前做一些操作,可以在CBV中定义dispatch方法来拦截
        def dispatch(self, request, *args, **kwargs):
        
            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)
        

4. CBV加装饰器方法(装饰器‘outter’)

    a. 直接写在函数上方
    
        from django.views import View
        from django.utils.decorators import method_decorator
        
        
        class Mylogin(View):
                                        
            @method_decorator(outter)
            def get(self, request):
                print('我是MyLogin里面的get方法')
                return render(request, 'login.html')
                                
            def post(self, request):
                print('我是MyLogin里面的post方法')
                return HttpResponse('post')
        
    
    b. 写在类上方指定给某个方法装
    
        from django.views import View
        from django.utils.decorators import method_decorator
        
        
        @method_decorator(outter, name='post')
        class Mylogin(View):
                                         
                def get(self, request):
                        print('我是MyLogin里面的get方法')
                        return render(request, 'login.html')
                                
                def post(self, request):
                        print('我是MyLogin里面的post方法')
                        return HttpResponse('post')
        
    c. 给dispatch方法装
    
        from django.views import View
        from django.utils.decorators import method_decorator
        
        
        @method_decorator(outter, name='dispatch')
        class Mylogin(View):
                
                def dispatch(self, request, *args, **kwargs):
                        return super().dispatch(request, *args, **kwargs)
                
                def get(self, request):
                        print('我是MyLogin里面的get方法')
                        return render(request, 'login.html')
                                
                def post(self, request):
                        print('我是MyLogin里面的post方法')
                        return HttpResponse('post')

你可能感兴趣的:(四、视图层)