zoukankan      html  css  js  c++  java
  • Django FBV和CBV

    一、FBV和CBV

      在Python菜鸟之路:Django 路由、模板、Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view 。

      今天补充另外一种路由关系的写法:CBV,即:class base view , 也可以看做为面向资源编程的另外一种叫法,类似tornado中的路由写法。

    1. 建立路由关系urls.py

    1
    2
    3
    4
    5
    from app01 import views
     
    urlpatterns = [
        url(r'^home/', views.Home.as_views()),
    ]

    2. 书写处理逻辑views.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from django.views import View
    class Home(View):  # 这里需要注意,必须继承View类
         # dispatch可以不写,仅仅作为了解,明白在所有类中,优先会执行dispatch方法,便于扩展
        def dispatch(self, request, *args, **kwargs):
            # 调用父类中的dispatch
            print('before')  # 类似装饰器的功能
            result = super(Home,self).dispatch(request, *args, **kwargs)
            print('after')  # 类似装饰器的功能
            return result
     
        def get(self,request):
            print(request.method)
            return render(request, 'home.html')
     
        def post(self,request):
            print(request.method,'POST')
            return render(request, 'home.html')

    二、url中的默认参数urls.py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    urlpatterns = [
        url(r'^index/', views.index, name='root'),
    ]
     
    或者
     
    urlpatterns = [
        url(r'^index/', views.index, {'name':'root',}),
    ]

      对应地,在views.py中,函数也需要有一个参数来接收默认参数

    1
    2
    3
    def index(request,name):
        print(name)
        return HttpResponse('OK')

    三、FBV和CBV的用户验证装饰器

      FBV简单,就是通常所用到的函数的装饰器。而CBV的用户验证,可以用上面提到的dispatch方法,也可以用另外一种方法,请往下看

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    # FBV的用户验证装饰器
    def auth(func):
        def inner(reqeust,*args,**kwargs):
            = reqeust.COOKIES.get('username111')
            if not v:
                return redirect('/login/')
            return func(reqeust, *args,**kwargs)
        return inner
     
    @auth
    def index(reqeust):
        # 获取当前已经登录的用户
        = reqeust.COOKIES.get('username111')
        return render(reqeust,'index.html',{'current_user': v})

    ------------------

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    from django import views
    from django.utils.decorators import method_decorator
     
    class Order(views.View):
        @method_decorator(auth)
        def get(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})
     
     
        def post(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})

      从上边可以发现一个特别,如果想对POST方法也进行认证,就需要在post函数上再加装饰器,如果有六七种方法,那么无疑需要六七种装饰器,是很麻烦的, 因此可以利用dispatch方法来进行验证,利用了所有的class都会执行dispatch方法的特性

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    from django import views
     
    class Order(views.View):
     
        @method_decorator(auth)
        def dispatch(self, request, *args, **kwargs):
            return super(Order,self).dispatch(request, *args, **kwargs)
     
        def get(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})
     
        def post(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})

      某些人可能在想了,这样还得写一个dispatch函数,而实际上dispatch函数内容什么也都没变,那么有没有更加简便的方法,最终版看如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    from django import views
    from django.utils.decorators import method_decorator
     
    @method_decorator(auth,name='dispatch')
    class Order(views.View):
        def get(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})
     
        def post(self,reqeust):
            = reqeust.COOKIES.get('username111')
            return render(reqeust,'index.html',{'current_user': v})

      

  • 相关阅读:
    DirectX:在graph自己主动连线中增加自己定义filter(graph中遍历filter)
    C3P0数据库连接池使用
    POJ
    【jQuery】复选框的全选、反选,推断哪些复选框被选中
    BestCoder Round #75 King's Cake 模拟&&优化 || gcd
    《Javascript_Dom 编程艺术》(第2版)读书笔记
    POJ 2947-Widget Factory(高斯消元解同余方程式)
    MFC 小知识总结四
    迭代器和iter()函数
    hdu1595find the longest of the shortest 最短路
  • 原文地址:https://www.cnblogs.com/ExMan/p/10433209.html
Copyright © 2011-2022 走看看