Django框架中,如何区分FBV和CBV两种函数式视图与类视图?
- 内容介绍
- 文章标签
- 相关推荐
本文共计500个文字,预计阅读时间需要2分钟。
Django请求处理方式有两种:FBV(Function-Based Views)和CBV(Class-Based Views)。
1. FBV:在视图中使用函数处理请求。 代码示例: python from django.conf.urls import url from . import views
urlpatterns=[ url(r'^index/$', views.index, name='index'), ]
2. CBV:在视图中使用类处理请求。 代码示例: python from django.conf.urls import url from django.views import generic
urlpatterns=[ url(r'^index/$', generic.ListView.as_view(), name='index'), ]
django中请求处理方式有2种:FBV 和 CBV
一、FBV
FBV(function base views)就是在视图里使用函数处理请求。
看代码:
urls.py
from django.conf.urls import url, include# from django.contrib import admin
from mytest import views
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index),
]
views.py
from django.shortcuts import renderdef index(req):
if req.method == ‘POST‘:
print(‘method is :‘ + req.method)
elif req.method == ‘GET‘:
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
注意此处定义的是函数
index.html
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="A" />
<input type="submit" name="b" value="提交" />
</form>
</body>
</html>
上面就是FBV的使用。
二、CBV
CBV(class base views)就是在视图里使用类处理请求。
将上述代码中的urls.py 修改为如下:
from mytest import viewsurlpatterns = [
# url(r‘^index/‘, views.index),
url(r‘^index/‘, views.Index.as_view()),
]
注:url(r‘^index/‘,views.Index.as_view()), 是固定用法。
将上述代码中的views.py 修改为如下:
from django.views import Viewclass Index(View):
def get(self, req):
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
def post(self, req):
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
注:类要继承View ,类中函数名必须小写。
本文共计500个文字,预计阅读时间需要2分钟。
Django请求处理方式有两种:FBV(Function-Based Views)和CBV(Class-Based Views)。
1. FBV:在视图中使用函数处理请求。 代码示例: python from django.conf.urls import url from . import views
urlpatterns=[ url(r'^index/$', views.index, name='index'), ]
2. CBV:在视图中使用类处理请求。 代码示例: python from django.conf.urls import url from django.views import generic
urlpatterns=[ url(r'^index/$', generic.ListView.as_view(), name='index'), ]
django中请求处理方式有2种:FBV 和 CBV
一、FBV
FBV(function base views)就是在视图里使用函数处理请求。
看代码:
urls.py
from django.conf.urls import url, include# from django.contrib import admin
from mytest import views
urlpatterns = [
# url(r‘^admin/‘, admin.site.urls),
url(r‘^index/‘, views.index),
]
views.py
from django.shortcuts import renderdef index(req):
if req.method == ‘POST‘:
print(‘method is :‘ + req.method)
elif req.method == ‘GET‘:
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
注意此处定义的是函数
index.html
<!DOCTYPE html><html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="A" />
<input type="submit" name="b" value="提交" />
</form>
</body>
</html>
上面就是FBV的使用。
二、CBV
CBV(class base views)就是在视图里使用类处理请求。
将上述代码中的urls.py 修改为如下:
from mytest import viewsurlpatterns = [
# url(r‘^index/‘, views.index),
url(r‘^index/‘, views.Index.as_view()),
]
注:url(r‘^index/‘,views.Index.as_view()), 是固定用法。
将上述代码中的views.py 修改为如下:
from django.views import Viewclass Index(View):
def get(self, req):
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
def post(self, req):
print(‘method is :‘ + req.method)
return render(req, ‘index.html‘)
注:类要继承View ,类中函数名必须小写。

