django系列3.3--CBV 和 FBV

一.CBV和FBV

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

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.pypython

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

urls.pydjango

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

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

<br/> CBV传参,和FBV相似,有名分组,无名分组app

第一种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属性的值会被覆盖掉 类写法post

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)url

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

<br/>spa

二.给视图函数加装饰器

写一个装饰器:code

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

<br/>htm

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")

<br/>

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'))

<br/>

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')
相关文章
相关标签/搜索