前面学习的例子都是经过 url 来匹配 一个函数,这种方式叫作 function based view (BSV)。一个典型的使用方式就是经过在view.py里面定义一个函数,而后经过函数的request参数获取method的类型,好比直接刷新页面就是get方式,提交表单就是post方式,这样来根据提交的不一样方式进行不一样的处理。html
好比:python
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方法的本质是一个反射器,能够根据名字来调用同名的方法。django
View类的部分代码能够看出dispatch经过反射获取列表里面的方法名称app
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的例子以下。ide
注意这里在子类里面从新定义了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()方法做为入口。post
好比说学习
# urls.py from django.conf.urls import url from myapp.views import MyView urlpatterns = [ url(r'^about/', MyView.as_view()), ]