Search code examples
pythondjangodjango-rest-frameworkdjango-generic-viewsdjango-pagination

django rest framework global pagination not working with ListCreateAPIView


I have the following in my settings.py

REST_FRAMEWORK = {
     'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
     'PAGE_SIZE': 50
       }

urls.py

url(r'^dashboard/users$', views.UserList.as_view()),

And the View itself

class UserList(generics.ListCreateAPIView):
     queryset = User.objects.all()
     serializer_class = UserSerializer

When i try to access /dashboard/users/?page=1 I get a 404 error with the following urls in the debug mode:

^dashboard/users$
^dashboard/users\.(?P<format>[a-z0-9]+)/?$

According to the Django rest frameworks's pagination docs:

Pagination is only performed automatically if you're using the generic views or viewsets. If you're using a regular APIView, you'll need to call into the pagination API yourself to ensure you return a paginated response. See the source code for the mixins.ListModelMixin and generics.GenericAPIView classes for an example.

I am already using generic views here, then why doesn't this work?


Solution

  • Apart from the helpfull suggestion from @neverwalkaloner , I Was still seeing a 404 error. I turned out that is was due to a url missmatch

    I had to change my url definition from

    url(r'^dashboard/users$', views.UserList.as_view())
    

    to

    url(r'^dashboard/users/$', views.UserList.as_view())
    

    The trailing / did the trick