django系列3.3--CBV 和 FBV

一.CBV和FBV

FBV function base views 用函数方法来处理请求

	from django.http import HttpResponse
  
	def my_view(request):
	if request.method == 'GET':
        return HttpResponse('OK')
	
	CBV class base views 用类来处理请求(面向对象)
	
	from django.http import HttpResponse
	from django.views import View
  
	class MyView(View):
	
		def get(self, request):
			return HttpResponse('OK')

CBV class base views 用类来处理请求(面向对象)
views.py

	from django.http import HttpResponse
	from django.views import View
  
	class MyView(View):
	
		def get(self, request):
			return HttpResponse('OK')

urls.py

from django.conf.urls import url
from myapp.views import MyView #引入我们在views.py里面创建的类

urlpatterns = [
     url(r'^index/$', MyView.as_view()),
]

CBV传参,和FBV类似,有名分组,无名分组

第一种url写法:无名分组的

url(r'^cv/(\d{2})/', views.MyView.as_view(),name='cv'),
url(r'^cv/(?P<n>\d{2})/', views.MyView.as_view(name='xxx'),name='cv')     

如果想给类的name属性赋值,前提你的MyView类里面必须有name属性(类属性,定义init方法来接受属性行不通,但是可以自行研究一下,看看如何行通,意义不大),并且之前类里面的name属性的值会被覆盖掉
类写法

class MyView(View):
    name = 'robertx'

    def get(self,request,n):
        print('get方法执行了')
        print('>>>',n)
        return render(request,'cvpost.html',{'name':self.name})

    def post(self,request,n):
        print('post方法被执行了')
        return HttpResponse('post')

第二种url写法: 也可以在url中指定类的属性(在url中设置类的属性Python)

urlpatterns = [
	url(r'^index/$', MyView.as_view(name="robertx")),   # 类里面必须有name属性,并且会被传进来的这个属性值给覆盖掉
]   

二.给视图函数加装饰器

写一个装饰器:

	def wrapper(func):
		def inner(*args, **kwargs):
			start_time = time.time()
			ret = func(*args, **kwargs)
			end_time = time.time()
			print("used:", end_time-start_time)
		return ret
	return inner

1.使用装饰器装饰FBV

	#添加班级
	@wrapper
	def add_class(request):
		if request.method == "POST":
			class_name = request.POST.get("class_name")
			models.Classes.objects.create(name=class_name)	
			return redirect("/class_list/")
		return render(request, "add_class.html")

2.使用装饰器装饰CBV

	from django.views import View
	from django.utils.decorators import method_decorator
		
	class AddClass(View):
		
		@method_decorator(wrapper)
		def get(self, request):
			return render(request, "add_class.html")
			
		def post(self, request):
			class_name = request.POST.get("class_name")
			return redirect("/class_list/")

总体来说有三种方式:

from django.utils.decorators import method_decorator
from django.views import View

@method_decorator(wrapper,name='get')#CBV版装饰器方式一
class BookList(View):
    @method_decorator(wrapper) #CBV版装饰器方式二
    def dispatch(self, request, *args, **kwargs):
        print('请求内容处理开始')
        res = super().dispatch(request, *args, **kwargs)
        print('处理结束')
        return res
    def get(self,request):
        print('get内容')
        # all_books = models.Book.objects.all()
        return render(request,'login.html')
    @method_decorator(wrapper) #CBV版装饰器方式三
    def post(self,request):
        print('post内容')
        return redirect(reverse('book_list'))

3.dispatch() 分发的方式处理函数

请求过来之后会先执行dispatch()方法,如果需要批量对具体的请求处理方法.如get,post等做一些操作的时候,可以手动改写dispatch方法,这个dispatch方法就和在FBV上加装饰器一样

class Login(View):    
	def dispatch(self, request, *args, **kwargs):
    	print('before')
        obj = super(Login,self).dispatch(request, *args, **kwargs)
        print('after')
        return obj
 
    def get(self,request):
        return render(request,'login.html')
     
    def post(self,request):
        print(request.POST.get('user'))
        return HttpResponse('Login.post')

posted @ 2019-03-03 16:04  robertx  阅读(257)  评论(0编辑  收藏  举报