【发布时间】:2018-05-25 22:01:46
【问题描述】:
Views.py
class CountryViewSet(viewsets.ViewSet):
serializer_class = CountrySerializer
pagination_class = LimitOffsetPagination
def list(self,request):
try:
country_data = Country.objects.all()
country_serializer = CountrySerializer(country_data,many=True)
return Response(
data = country_serializer.data,
content_type='application/json',
)
except Exception as ex:
return Response(
data={'error': str(ex)},
content_type='application/json',
status=status.HTTP_400_BAD_REQUEST
)
设置.py 我已经添加了
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
在我的 urls.py 中
router = routers.DefaultRouter(trailing_slash=False)
router.register(r'country', CountryViewSet, base_name='country')
urlpatterns = [
url(r'^', include(router.urls)),
]
当我尝试使用此 URL http://192.168.2.66:8001/v1/voucher/country 时,它会返回所有数据。
但是当我尝试使用这个 URL http://192.168.2.66:8001/v1/voucher/country/?limit=2&offset=2
但它返回 404 错误。 我是 django 的新手。请帮助我:)
【问题讨论】:
-
您肯定会得到 404,因为您设置了 trailing_slash=False 但您在 URL 中使用了斜杠。这与偏移量无关。
标签: django django-rest-framework drf-queryset