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

    FBV + CBV

    django中请求处理方式有2种:FBV 和 CBV

    FBV(function bases views)

    就是在视图里使用函数处理请求,如下:

    # urls.py
    
    from django.conf.urls import url, include
    from app01 import views
     
    urlpatterns = [
        url(r'^index/', views.index),
    ]
    
    # views.py
    
    from django.shortcuts import render
     
    def index(req):
        if req.method == 'POST':
            print('method is :' + req.method)
        elif req.method == 'GET':
            print('method is :' + req.method)
        return render(req, 'index.html')
    

    注意此处定义的是函数【def index(req):】

    <!--index.html-->
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>index</title>
    </head>
    <body>
        <form action="" method="post">
            <input type="text" name="A" />
            <input type="submit" name="b" value="提交" />
        </form>
    </body>
    </html>
    

    FBV中加装饰器相关

    def deco(func):
        def wapper(request,*agrs,**kwargs):
            if request.COOKIES.get('LOGIN'):
                return func(request, *args, **kwargs)     
            return redirect('/login/')
        return wrapper
    
    
    @deco
    def index(req):
        if req.method == 'POST':
            print('method is :' + req.method)
        elif req.method == 'GET':
            print('method is :' + req.method)
        return render(req, 'index.html')
    
    

    上面就是FBV的使用。

    CBV(class bases views)

    就是在视图里使用类处理请求,如下:

    # urls.py
    
    from app01 import views
     
    urlpatterns = [
        url(r'^index/', views.Index.as_view()),
    ]
    
    # views.py
    
    from django.views import View
     
    # 类要继承View ,类中函数名必须小写
    class Index(View):
        def get(self, req):
            '''
            处理GET请求
            '''
            print('method is :' + req.method)
            return render(req, 'index.html')
     
        def post(self, req):
            '''
            处理POST请求
            '''
            print('method is :' + req.method)
            return render(req, 'index.html')
    

    CBV中加装饰器相关

    要在CBV视图中使用我们上面的check_login装饰器,有以下三种方式:

    1. 加在CBV视图的get或post方法上

      from django.utils.decorators import method_decorator
      from django.views import View
      
      class Index(View):
          
          def dispatch(self,req,*args,**kwargs):
              return super(Index,self).dispatch(req,*args,**kwargs)
          
          
          def get(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
          
       	@method_decorator(deco)
          def post(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
          
      
    2. 加在diapatch方法上,因为CBV中首先执行的就是dispatch方法,所以这么写相当于给get和post方法都加上了登录校验。

      from django.utils.decorators import method_decorator
      from django.views import View
      
      class Index(View):
          
          @method_decorator(deco)
          def dispatch(self,req,*args,**kwargs):
              return super(Index,self).dispatch(req,*args,**kwargs)
          
          
          def get(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
          
          def post(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
          
      
    3. 直接加在视图类上,但method_decorator必须传name关键字参数

      如果get方法和post方法都需要登录校验的话就写两个装饰器

      from django.utils.decorators import method_decorator
      from django.views import View
      
      
      @method_decorator(deco,name='get')
      @method_decorator(deco,name='post')
      class Index(View):
      
          def dispatch(self,req,*args,**kwargs):
              return super(Index,self).dispatch(req,*args,**kwargs)
          
          
          def get(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
          
          def post(self, req):
              print('method is :' + req.method)
              return render(req, 'index.html')
      
  • 相关阅读:
    3.流程控制语句
    pandas数据处理(一)pymongo数据库量大插入时去重速度慢
    windows下载安装redis
    Scrapy(一)爬知乎所有用户信息
    Mongodb去除重复的数据,pymongo去重
    selenium爬虫报错:Message: stale element reference: element is not attached to the page document 促成1分钟爬完斗鱼主播信息。
    用pyspider爬斗鱼主播信息
    曼城新闻情报站(二)Django框架的爬虫
    曼城新闻情报站(一)爬取3大网站的曼城新闻
    Beautiful Soup多线程爬取斗鱼所有主播信息(改良版)
  • 原文地址:https://www.cnblogs.com/Hades123/p/11379067.html
Copyright © 2011-2022 走看看