Django学习——viewsets和router完成商品列表页

views.py中的代码

from rest_framework.pagination import PageNumberPagination

from goods.models import Goods
from goods.serializers import GoodsSerializer

from rest_framework import viewsets
from rest_framework import mixins

class GoodsPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    page_query_param = "p"
    max_page_size = 100

#viewsets.GenericViewSet没有get方法
class GoodsListViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    商品列表页
    """
    queryset = Goods.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = GoodsPagination

urls.py中的代码:

from goods.views import GoodsListViewSet
from rest_framework.routers import DefaultRouter

router = DefaultRouter()
router.register(r'goods', GoodsListViewSet)



urlpatterns = [


    #商品列表页
    url(r'^', include(router.urls)),
]

猜你喜欢

转载自blog.csdn.net/qq_37002901/article/details/84766343