首页 技术 正文
技术 2022年11月9日
0 收藏 599 点赞 3,888 浏览 2825 个字

一.CBV和FBV

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

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

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

views.py

from django.http import HttpResponse
from django.views import Viewclass 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_decoratorclass 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')
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,497
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,910
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,744
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,497
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,135
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,298