1、CBV和FBV
全称应该是class base views 和function base views
理解起来应该就是基于类的视图函数和基于函数的视图函数html
FBV
应该是我目前最经常使用的一种方式了,就是给每个views里的功能添加本身专用的方法。例如若是要对网页进行get访问,而后经过得到request中post方式传递的form表单获取数据。shell
from django.http import HttpResponse def login(request): if request.method == 'GET': return HttpResponse('OK1') if request.method =="POST": return HttpResponse('OK2')
。。。
CBV
可是这种方法,看起来有点臃肿,查看代码的时候不容易看清楚你的post请求get请求是在哪里处理的,因此就有了CBV的处理方法。django
在views文件中:app
from django.views import View class LoginView(View): def get(self,request): return render(request,"login.html") def post(self,request): user=request.POST.get('user') pwd=request.POST.get('pwd')) if Turn: #假使验证成立 return HttpResponse("OK3")
在CBV的使用中,须要调用父类View
,它会在源码里解释这个CBV的应用范围,以及运做原理。函数
在urls文件中:post
from app01 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/$',views.LoginView.as_view()),
# url(r'^login/$',views.login) ]
综上所述:
咱们能够知道设置一个CBV方法,要作的就是,在views里建立一个类,这个类的父类必定得是View
,并且在urls设置的时候,url指向的再也不是一个函数名,而是你定义的类的.as_view()
方法url
运行起来以后,会发现当向login.html这个url发送get请求的时候,成功,发送post请求的时候,也成功,而且有相应的返回值。spa
2、原理
翻看源码,实现以上功能的核心,其实都在那个被继承的父类View
里:code
class View(object): """ Intentionally simple parent class for all views. Only implements dispatch-by-method and simple sanity checking. """ http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'] def __init__(self, **kwargs): """ Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things. """ # Go through keyword arguments, and either save their values to our # instance, or raise an error. for key, value in six.iteritems(kwargs): setattr(self, key, value) @classonlymethod def as_view(cls, **initkwargs): """ Main entry point for a request-response process. """ for key in initkwargs: if key in cls.http_method_names: raise TypeError("You tried to pass in the %s method name as a " "keyword argument to %s(). Don't do that." % (key, cls.__name__)) if not hasattr(cls, key): raise TypeError("%s() received an invalid keyword %r. as_view " "only accepts arguments that are already " "attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs): self = cls(**initkwargs) if hasattr(self, 'get') and not hasattr(self, 'head'): self.head = self.get self.request = request self.args = args self.kwargs = kwargs return self.dispatch(request, *args, **kwargs) view.view_class = cls view.view_initkwargs = initkwargs # take name and docstring from class update_wrapper(view, cls, updated=()) # and possible attributes set by decorators # like csrf_exempt from dispatch update_wrapper(view, cls.dispatch, assigned=()) return view 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) def http_method_not_allowed(self, request, *args, **kwargs): logger.warning( 'Method Not Allowed (%s): %s', request.method, request.path, extra={'status_code': 405, 'request': request} ) return http.HttpResponseNotAllowed(self._allowed_methods()) def options(self, request, *args, **kwargs): """ Handles responding to requests for the OPTIONS HTTP verb. """ response = http.HttpResponse() response['Allow'] = ', '.join(self._allowed_methods()) response['Content-Length'] = '0' return response def _allowed_methods(self): return [m.upper() for m in self.http_method_names if hasattr(self, m)]
从源码中咱们能够看到。这个CBV的核心类,是为了处理各类请求服务的。其中有一个list来存放这些请求,而且指向他们应该实现的函数功能。
再补充一点,在CBV建立类方法的时候,必定要携带一个request参数。而这个参数里面就携带了request.method.lower()
,经过反射,CBV函数天然能处理这些method对应的请求。orm
3、逻辑过程
看源码的准则就看,看本身看的懂得代码,应为源码的有些高深的设置,咱们先阶段还不须要去了解.
首先,无论cbv仍是fbv,在url中都是用户访问请求,才回去执行相应的视图函数,
cbv在启动项目的时候,已经在LoginView中执行一段骚操做的获得一个函数名,咱们如今就来看看:
首先咱们要明确的一点就是一个类.属性或方法,首先在本身那找,找不到继承的父类找。因此不能直接点。as_view,
先去类LoginView中找看看有,有没有方法:
点开View,咱们找到as_view方法,发现是类方法,
前面过程作了什么咱们无论,返回值只是返回一个view,调用view函数,咱们去看view函数,
view函数最后返回self.dispacth,这里要注意!!!这个self.dispacth,self是谁,self咱们看代码发现是LoginView类,
仍是那个准则,调用属性方法,如今本身那边找,没有再找父类。
由于LoginView中没有dispath这方法,因此仍是执行的父类的dispath方法:
注意点:仍是在调用某个方法时,咱们必定要肯定是谁去调用这个方法!找方法必定先找本身的,没有再去父类找!
4、思考自定义的dispatch
既然在上面咱们查看源码的时候已经发现,导向专门的method操做的函数是dispatch,并且每一个CBV类的父类都是View,那我能不能在这个dispatch里面作一些定制化操做,
-
继承父类的dispatch
def dispatch(self, request, *args, **kwargs): obj=super(login,self).dispatch(request, *args, **kwargs) return obj # 因为父类的dispatch最后返回了一个handle,也就是一个返回值,因此在继承的时候也应该提供一个返回值
-
装饰化这个dispatch
def dispatch(self, request, *args, **kwargs): print('123‘) obj=super(login,self).dispatch(request, *args, **kwargs) print('456') return obj
反馈的效果:
不论是什么方法请求,都必需要打印123,456
总的来讲,这个只是一个简单的示范处理,若是须要对过来的请求作更多的润色,还须要在这个继承动做先后作更多工做。须要知道的是,他和装饰器略微不懂,那就是他能够共享这个dispatch的request,而且对他进行工做