用viewsets设置url

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)