Search code examples
djangodjango-rest-frameworkrenderer

Django REST Framework - let ListAPIView behave similar to pure ListView


I am trying to convince ListAPIView to behave the way the ListView from pure Django does (that means, renders a template with an object_list variable, possibly some pagination stuff, and so on). This is what I tried:

class UserListView(ListAPIView):
    permission_classes = (AllowAny, )
    queryset = User.objects.all()
    serializer_class = UserListSerializer
    renderer_classes = (TemplateHTMLRenderer, )
    template_name = 'user/list.html'

Assume the User to be the builtin Django user model, UserListSerializer to be a ModelSerializer with fields = "__all__" and the template containing just a for loop over object_list displaying all the users.

When I try it, I get the following error:

TypeError: context must be a dict rather than ReturnList.

I must be doing something terribly wrong, I believe there must me a way to make use of the genericity and I just have no idea how.


Solution

  • Ok, I just stepped into a trap. Everything works as expected if I enable pagination, e. g. adding this into my settings:

    REST_FRAMEWORK = {
        'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
        'PAGE_SIZE': 100
    }
    

    I would never guess this would be the problem.