Django FBV和CBV -
一、FBV和CBV
在Python菜鸟之路:Django 路由、模板、Model(ORM)一节中,已经介绍了几种路由的写法及对应关系,那种写法可以称之为FBV: function base view 。
今天补充另外一种路由关系的写法:CBV,即:class base view , 也可以看做为面向资源编程的另外一种叫法,类似tornado中的路由写法。
1. 建立路由关系urls.py
1
2
3
4
5
|
from app01 import views urlpatterns = [ url(r '^home/' , views.Home.as_views()), ] |
2. 书写处理逻辑views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
from django.views import View class Home(View): # 这里需要注意,必须继承View类 # dispatch可以不写,仅仅作为了解,明白在所有类中,优先会执行dispatch方法,便于扩展 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中的默认参数urls.py
1
2
3
4
5
6
7
8
9
|
urlpatterns = [ url(r '^index/' , views.index, name = 'root' ), ] 或者 urlpatterns = [ url(r '^index/' , views.index, { 'name' : 'root' ,}), ] |
对应地,在views.py中,函数也需要有一个参数来接收默认参数
1
2
3
|
def index(request,name): print (name) return HttpResponse( 'OK' ) |
三、FBV和CBV的用户验证装饰器
FBV简单,就是通常所用到的函数的装饰器。而CBV的用户验证,可以用上面提到的dispatch方法,也可以用另外一种方法,请往下看
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# FBV的用户验证装饰器 def auth(func): def inner(reqeust, * args, * * kwargs): v = reqeust.COOKIES.get( 'username111' ) if not v: return redirect( '/login/' ) return func(reqeust, * args, * * kwargs) return inner @auth def index(reqeust): # 获取当前已经登录的用户 v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) |
------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
|
from django import views from django.utils.decorators import method_decorator class Order(views.View): @method_decorator (auth) def get( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) def post( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) |
从上边可以发现一个特别,如果想对POST方法也进行认证,就需要在post函数上再加装饰器,如果有六七种方法,那么无疑需要六七种装饰器,是很麻烦的, 因此可以利用dispatch方法来进行验证,利用了所有的class都会执行dispatch方法的特性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
from django import views class Order(views.View): @method_decorator (auth) def dispatch( self , request, * args, * * kwargs): return super (Order, self ).dispatch(request, * args, * * kwargs) def get( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) def post( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) |
某些人可能在想了,这样还得写一个dispatch函数,而实际上dispatch函数内容什么也都没变,那么有没有更加简便的方法,最终版看如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
from django import views from django.utils.decorators import method_decorator @method_decorator (auth,name = 'dispatch' ) class Order(views.View): def get( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) def post( self ,reqeust): v = reqeust.COOKIES.get( 'username111' ) return render(reqeust, 'index.html' ,{ 'current_user' : v}) |
posted on 2019-02-25 20:35 ExplorerMan 阅读(179) 评论(0) 编辑 收藏 举报