Django REST & react-admin: Content-Range header missing

Using react-admin's ra-data-simple-rest DataProvider I would like to make a request to a django REST api. In Django I built the following custom pagination class which is supposed to expose the Content-Range header required by the DataProvider:

from rest_framework import pagination
from rest_framework.response import Response

class ContentRangeHeaderPagination(pagination.PageNumberPagination):

    def get_paginated_response(self, data):

        total_items = self.page.paginator.count # total no of items in queryset
        item_starting_index = self.page.start_index() - 1 # In a page, indexing starts from 1
        item_ending_index = self.page.end_index() - 1

        content_range = 'posts: {0}-{1}/{2}'.format(item_starting_index, item_ending_index, total_items) 

        headers = {'Content-Range': content_range} 

        return Response(data, headers=headers)

In Django settings I put the following:

INSTALLED_APPS = [
    ...
    'corsheaders',
    'rest_framework',
]


MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': '<myProject>.pagination.ContentRangeHeaderPagination',
    'PAGE_SIZE': 10
}

CORS_ALLOWED_ORIGINS = [
    'http://127.0.0.1:5173',
]

CORS_ALLOW_HEADERS = (
    "accept",
    "range",
    "content-range",
    "authorization",
    "content-type",
    "user-agent",
    "x-csrftoken",
    "x-requested-with",
)

I keep getting the Error "The Content-Range header is missing in the HTTP Response. The simple REST data provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?"

Any ideas what I am doing wrong?

Unfortunately the ra-data-django-rest-framework Data Provider is pretty outdated and I am getting quite some dependency conflicts trying to install it.

Вернуться на верх