“GenericView方式实现商品列表页和分页功能详解”的版本间的差异
来自CloudWiki
(创建页面,内容为“==视图层views.py== <nowiki> from .serializers import GoodsSerializer from rest_framework.response import Response from rest_framework import mixins from rest_fr…”) |
|||
第94行: | 第94行: | ||
</nowiki> | </nowiki> | ||
+ | ==测试分页== | ||
+ | 在浏览器输入:http://10.0.0.30:8000/goods/ | ||
[[文件:bd20-5-63.png|600px]] | [[文件:bd20-5-63.png|600px]] | ||
+ | |||
+ | 并且,现在前端人员可根据需要,灵活的确定页数 和每页大小, | ||
+ | |||
+ | 如:http://10.0.0.30:8000/goods/?page=1&page_size=20 | ||
+ | |||
+ | [[文件:bd20-5-64.png|600px]] |
2020年8月2日 (日) 04:10的最新版本
视图层views.py
from .serializers import GoodsSerializer from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from .models import Goods # Create your views here. class GoodsListView(generics.ListAPIView): """ 商品列表页 """ queryset = Goods.objects.all()[:10] serializer_class = GoodsSerializer
测试
在浏览器输入:http://10.0.0.30:8000/goods/
"results": [ { "id": 1, "category": { "id": 20, "name": "根茎类", "code": "gjl", "desc": "", "category_type": 2, "is_tab": false, "add_time": "2020-06-24T16:34:11", "parent_category": 1 }, "goods_sn": "", "name": "新鲜水果甜蜜香脆单果约800克", "click_num": 0, "sold_num": 0, "fav_num": 0, "goods_num": 0, "market_price": 232.0, "shop_price": 156.0, "goods_brief": "食用百香果可以增加胃部饱腹感,减少余热量的摄入,还可以吸附胆固醇和胆汁之类有机分子,抑制人体对脂肪的吸收。因此,长期食用有利于改善人体营养吸收结构,降低体内脂肪,塑造健康优美体态。", "goods_desc": "<p><img src=\"/media/goods/images/2_20170719161405_249.jpg\" title=\"\" alt=\"2.jpg\"/></p><p><img src=\"/media/goods/images/2_20170719161414_628.jpg\" title=\"\" alt=\"2.jpg\"/></p><p><img src=\"/media/goods/images/2_20170719161435_381.jpg\" title=\"\" alt=\"2.jpg\"/></p>", "ship_free": true, "goods_front_image": "http://10.0.0.30:8000/media/goods/images/1_P_1449024889889.jpg", "is_new": false, "is_hot": false, "add_time": "2020-06-24T18:01:07" },
特别注意:"goods_front_image": "http://10.0.0.30:8000/media/goods/images/1_P_1449024889889.jpg",
分页功能
参考官网,编写分页类、并引用
views.py:
from .serializers import GoodsSerializer from rest_framework.response import Response from rest_framework import mixins from rest_framework import generics from rest_framework.pagination import PageNumberPagination from .models import Goods # Create your views here. class GoodsPagination(PageNumberPagination): page_size = 12 page_size_query_param = 'page_size' page_query_param = "page" max_page_size = 100 class GoodsListView(generics.ListAPIView): """ 商品列表页 """ queryset = Goods.objects.all() serializer_class = GoodsSerializer pagination_class = GoodsPagination
测试分页
在浏览器输入:http://10.0.0.30:8000/goods/
并且,现在前端人员可根据需要,灵活的确定页数 和每页大小,