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

django系列3.3--CBV 和 FBV的更多相关文章

  1. Django学习笔记之CBV和FBV

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...

  2. Django视图函数:CBV与FBV (ps:补充装饰器)

    CBV 基于类的视图  FBV 基于函数的视图 CBV: 1 项目目录下: 2 urlpatterns = [ 3 path('login1/',views.Login.as_view()) #.as ...

  3. Django(视图 CBV、FBV)

    day67 参考:http://www.cnblogs.com/liwenzhou/articles/8305104.html CBV和FBV 我们之前写过的都是基于函数的view,就叫FBV.还可以 ...

  4. Django中的CBV和FBV

    Django中的CBV和FBV 一.  CBV CBV是采用面向对象的方法写视图文件. CBV的执行流程: 浏览器向服务器端发送请求,服务器端的urls.py根据请求匹配url,找到要执行的视图类,执 ...

  5. django视图 CBV 和 FBV

    目录 视图 CBV 和 FBV 什么是视图? FBV function based view 基于函数的视图 CBV class based view 基于类的视图 小技巧 CBV 如何获取页面请求类 ...

  6. Django之CBV和FBV

    Django之CBV和FBV CBV和FBV是C和F的区别: C是Class,F是Function 在请求中,有GET请求和POST请求. 在写CBV时,url是可以对应一个类的,在类中,分别写出GE ...

  7. django补充CBV和FBV模式

    django补充CBV和FBV模式FBV模式---函数:经常用的方式CBV模式---类CBV中url连接时函数名后面要接.as_view()class index(views.View): @... ...

  8. Django框架(五)-- 视图层:HttpRequest、HTTPResponse、JsonResponse、CBV和FBV、文件上传

    一.视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...

  9. Django CBV和FBV

    Django CBV和FBV Django内部CBV内部接收方法操作: 1.通过客户端返回的请求头RequestMethod与RequesrtURL,会以字符串形式发送到服务器端. 2.取到值后通过d ...

随机推荐

  1. Rhythmk 学习 Hibernate 09 - Hibernate HQL

    1.初始数据 @Test public void test01() { Session session = null; try { session = HibernateUtil.getSession ...

  2. react之引用echarts

    react之引用echarts npm: npm install echarts --save 代码: import React, { Component } from 'react'; // 引入 ...

  3. js格式化时间 js格式化时间戳

    一个js格式化时间和js格式化时间戳的例子. 代码:/** * 时间对象的格式化; */Date.prototype.format = function(format) { /* * eg:forma ...

  4. 跟我学算法-吴恩达老师(超参数调试, batch归一化, softmax使用,tensorflow框架举例)

    1. 在我们学习中,调试超参数是非常重要的. 超参数的调试可以是a学习率,(β1和β2,ε)在Adam梯度下降中使用, layers层数, hidden units 隐藏层的数目, learning_ ...

  5. _kbhit() for linux

    传送门:http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html #include <stdio.h> #in ...

  6. javascript中所谓的“坑”收录

    坑一: // 反例myname = "global"; // 全局变量function func() { alert(myname); // "undefined&quo ...

  7. [转] 移动平台Html5的viewport使用经验

    转自:http://blog.csdn.net/wuruixn/article/details/8591989 问题描述 web页面采用html5技术实现,在系统登录页面中使用frameset.fra ...

  8. JAVA - 守护线程(Daemon Thread)

    转载自:http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html 在Java中有两类线程:用户线程 (User Thread) ...

  9. 如何快速简单粗暴地理解Python中的if __name__ == '__main__'

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

  10. wcf已知类型 known type

    .服务契约的定义 /* Copyright (c) 2014 HaiHui Software Co., Ltd. All rights reserved * * Create by huanglc@h ...