美文网首页
用viewsets设置url

用viewsets设置url

作者: sean_liu_01 | 来源:发表于2020-08-05 14:13 被阅读0次

    1、用viewsets设置url

    views.py文件

    class GoodsListViewset(mixins.ListModelMixin, viewsets.GenericViewSet):

    queryset = Goods.objects.all()

    serrializer_class = GoodsSerializer

    pagination_class = GoodsPagination

    urls.py文件

    goods_list = GoodsListViewSet.as_view({

    'get': 'list',

    })

    urlpatterns = [

    url(r'goods/', goods_list, name="goods_list"),

    ]

    2、用drf的router配置url(这种方法好,推荐使用,我也不知道为什么)

    urls.y文件:

    from goods.views import GoodsListViewSet

    router = DefaultRouter()

    # 配置goods的url

    router.register(r'goods', GoodsListViewSet)

    urlpatterns = [

    url(r'^', include(router.urls)),

    ]

    相关文章

      网友评论

          本文标题:用viewsets设置url

          本文链接:https://www.haomeiwen.com/subject/ovonrktx.html