类视图
简介
视图是一个可调用的对象,它接收一个请求然后返回一个响应,这个可调用对象可以不只是函数,Django提供一些可以用作视图的类
基于类的视图使用Python 对象实现视图,它提供除函数视图之外的另外一种方式
view
- 属性
http_method_names:添加未知的请求方法
- 方法
as_view():将类视图转换成可被调用的函数视图
dispatch(): 根据请求去匹配http_method_names下的对应函数去处理请求
http_method_not_allowed():返回405错误
- 实例
from django.views import View
class IndexView(View):
http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace','list']
def get(self,request,*args,**kwargs):
return HttpResponse(IndexView)
def post(self,request,*args,**kwargs):
data = request.POST
return HttpResponse(data)
#需要将list方法加入 http_method_names,否则会经过,dispatch,http_method_not_allowed():返回405错误
def list(self,request,*args,**kwargs):
return HttpResponse("ListView")
请求方式:
In [27]: import requests
In [28]: url = 'http://192.168.33.10:8080/dashboard/index/'
In [29]: res = requests.request('list',url)
In [30]: res.content
Out[30]: b'ListView'
数据分页
基于sql切片的方式分页
数据准备
In [3]: from django.contrib.auth.models import User
In [4]: for n in range(1,101):
...: User.objects.create_user('wanghui-{}'.format(n),'wanghui-{}@alibaba.com'.format(n),'123456')
In [5]: User.objects.all()[1:10]
类视图
class UserView(View):
#http_method_names = ['get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace','list']
def get(self,request,*args,**kwargs):
#展示数据
# queryset = User.objects.all()
# return HttpResponse(queryset)
try:
page = int(request.GET.get('page'))
except:
page = 1
if page < 1:
page = 1
per = 10
end = page * per
start = end - per
queryset = User.objects.all()[start:end]
data = [{"id":user.id,"username":user.username,"email":user.email} for user in queryset]
return JsonResponse(data,safe=False)
基于Paginator对象的分页
class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
属性
- Paginator.count 所有页面的objects总数
- Paginator.num_pages 页面总数
- Paginator.page_range 页码的范围,从1开始,例如[1, 2, 3, 4]
方法
- Paginator.page(number) 返回一个page对象,number, 当前显示的是第几页
Page对象
class Page(object_list, number, paginator)
方法
- Page.has_next() 如果有下一页,返回True
- Page.has_previous() 如果有上一页,返回 True
- Page.has_other_pages() 如果有上一面或下一页,返回True
- Page.next_page_number() 返回下一页的页码.如果不存在,抛出InvalidPage异常
- Page.previous_page_number() 返回上一页的页码.如果不存在,抛出InvalidPage异常
- Page.start_index() 返回当前页上的第一个对象,相对于分页列表的所有对象的序号
- Page.end_index() 返回当前页上的最后一个对象,相对于分页列表的所有对象的序号
创建用户小例子
from django.core.paginator import Paginator
class UserViewV2(View):
def get(self,request,*args,**kwargs):
queryset = User.objects.all()
paginator = Paginator(queryset,10)
try:
page = int(request.GET.get("page"))
except:
page = 1
if page < 1:
page = 1
page = paginator.page(page)
data = [{"id":user.id,"username":user.username,"email":user.email} for user in page.object_list]
return JsonResponse(data,safe=False)
def post(self,request,*args,**kwargs):
username = request.POST.get("username")
userpass = request.POST.get("userpass")
email = request.POST.get('email')
if not username or userpass or not email:
return JsonResponse({"errormsg":"参数有误!"})
user = User.objects.create(username,email,userpass)
return JsonResponse({"id":user.id,"username":user.username,"email":user.email})
请求方式
In [1]: import requests
In [2]: url = "http://192.168.33.10:8080/dashboard/userview/"
In [3]: data = {}
In [4]: data["username"] = "zhangsan"
In [5]: data["userpass"] = '123456'
In [6]: data["email"] = "zhangsan@alibaba.com"
In [7]: requests.post(url,data)
Out[7]: <Response [200]>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。