标签:decorator The 缓存 route parameter offset 请求 paginati 自动
-简单分页: -from rest_framework.pagination import PageNumberPagination -生成一个对象 -调用对象的.page.paginate_queryset(books, request, view=self) -会有返回结果:把结果序列化,返回 -四个参数: 每页的大小 page_size = api_settings.PAGE_SIZE 查询第几页参数 page_query_param = ‘page1‘ 可以指定每页的大小 page_size_query_param = None 每页最大显示多少 max_page_size = None -偏移分页 -from rest_framework.pagination import LimitOffsetPagination -生成一个对象 -调用对象的.page.paginate_queryset(books, request, view=self) -会有返回结果:把结果序列化,返回 -四个参数: 默认大小 default_limit = api_settings.PAGE_SIZE 查询的条数 limit_query_param = ‘limit‘ 偏移的条数 offset_query_param = ‘offset‘ 最多多少条 max_limit = None -加密分页: -from rest_framework.pagination import CursorPagination -生成一个对象 -调用对象的.page.paginate_queryset(books, request, view=self) -会有返回结果:把结果序列化,返回 -两个参数: 按什么排序 ordering = ‘-nid‘ page_size = 3
1 原始的url -url(r‘^books/‘, views.Book.as_view()), 2 视图类继承ViewSetMixin(as_view中必须传参数,字典形式) -url(r‘^books/‘, views.Book.as_view(‘get‘:‘get_all’)), 3 全自动路由(自动生成路由) from rest_framework import routers router=routers.DefaultRouter() # 两个参数,一个是匹配的路由,一个是视图中写的CBV的类 router.register(‘book‘,views.Book)
url(r‘‘, include(router.urls)),
1、eg,参考连接https://i.cnblogs.com/EditPosts.aspx?opt=1
from rest_framework.response import Response from rest_framework.viewsets import ViewSetMixin from rest_framework.versioning import QueryParameterVersioning,AcceptHeaderVersioning,NamespaceVersioning,URLPathVersioning class Book(APIView): versioning_class=URLPathVersioning def get(self, request,*args,**kwargs): response = MyResponse() # print(request.GET.get(‘version‘)) # print(request.query_params.get(‘version‘)) print(request.version) books = models.Book.objects.all() ret = myserial.BookSer(instance=books, many=True) response.msg = ‘查询成功‘ response.data = ret.data return Response(response.get_dic)
-单个视图缓存 from django.views.decorators.cache import cache_page @cache_page(5) def test_time(request): ctime=time.time() return HttpResponse(ctime) -全局缓存 在中间件中加入: MIDDLEWARE = [ # 相应的时候,放入缓存 ‘django.middleware.cache.UpdateCacheMiddleware‘, 。。。 # 从缓存中取出页面 ‘django.middleware.cache.FetchFromCacheMiddleware‘ ] CACHE_MIDDLEWARE_SECONDS=10 -模板层的缓存 {%load cache %} {% cache 5 ‘time‘%} 应用缓存:{{ ctime }} {% endcache %} -缓存位置配置: -可以缓存到文件中 -可以缓存到内存中(默认就是内存) -可以缓存到redis中(需要安装django-redis模块) CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "redis://127.0.0.1:6379", "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", "CONNECTION_POOL_KWARGS": {"max_connections": 100} # "PASSWORD": "123", } } }
import time from django.views.decorators.cache import cache_page @cache_page(50) def test_time(request): ctime=time.time() # return HttpResponse(ctime) return render(request,‘test.html‘,locals())
标签:decorator The 缓存 route parameter offset 请求 paginati 自动
原文地址:https://www.cnblogs.com/di2wu/p/10153514.html