Django fbv 和 cbv 简述

发布时间:2020-06-26 15:03:44 作者:beanxyz
来源:网络 阅读:1785

前面学习的例子都是通过 url 来匹配 一个函数,这种方式叫做 function based view (BSV)。一个典型的使用方式就是通过在view.py里面定义一个函数,然后通过函数的request参数获取method的类型,比如直接刷新页面就是get方式,提交表单就是post方式,这样来根据提交的不同方式进行不同的处理。

比如:

def user_info(request):
    if request.method == "GET":
        user_list = models.UserInfo.objects.all()
        group_list = models.UserGroup.objects.all()
        return render(request, 'user_info.html', {'user_list': user_list, "group_list": group_list})
    elif request.method == 'POST':
        u = request.POST.get('user')
        p = request.POST.get('pwd')
        models.UserInfo.objects.create(username=u,password=p)
        return redirect('/cmdb/user_info/')

  

另外一种方式叫做 class based view (CBV)。这种方式需要导入一个View类,通过自定义一个子类继承这个类,我们可以利用View里面的dispatch()方法来判断请求是get还是post。dispatch方法的本质是一个反射器,可以根据名字来调用同名的方法。


View类的部分代码可以看出dispatch通过反射获取列表里面的方法名称

  http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace']
  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)


CBV的例子如下。


注意这里在子类里面重新定义了dispatch这个方法,同时又使用了super()来调用父类的构造方法,这样的好处是功能不变,但是可以灵活的添加新的功能。

from django.views import View
class Home(View):
    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里面,我们需要调用View类的as_view()方法作为入口。

比如说

# urls.py
from django.conf.urls import url
from myapp.views 
import MyView
urlpatterns = [
    
    url(r'^about/', MyView.as_view()),
]


推荐阅读:
  1. Django视图(View)
  2. Django使用CBV处理请求

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

django fbv cbv

上一篇:C# 计算器

下一篇:C 语言异常处理(五十二)

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》