Skip to content
Snippets Groups Projects
Commit 6888f31c authored by borzechof99's avatar borzechof99 :whale2:
Browse files

Fixes Pagination for react-admin to get data

parent 4b80bd61
No related branches found
No related tags found
No related merge requests found
from collections import OrderedDict
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
class ReactAdminPagination(PageNumberPagination):
page_query_param = "page"
page_size_query_param = "perPage"
def get_paginated_response(self, data):
count = self.page.paginator.count
item_starting_index = self.page.start_index() - 1
item_ending_index = self.page.end_index() - 1
content_range = "items {0}-{1}/{2}".format(
item_starting_index, item_ending_index, count
)
# headers = {"Content-Range": content_range}
headers = {"x-total-count": count}
return Response(
OrderedDict(
[
("x-total-count", count),
("next", self.get_next_link()),
("previous", self.get_previous_link()),
("results", data),
]
),
headers=headers,
)
class PageNumberWithPageSizePagination(PageNumberPagination):
page_size_query_param = "page_size"
# SOURCE: https://github.com/bmihelac/ra-data-django-rest-framework
......@@ -3,7 +3,7 @@ Django settings for unisportomat project.
Generated by 'django-admin startproject' using Django 3.2.
For more information on this file, see
For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/
For the full list of settings and their values, see
......@@ -32,7 +32,7 @@ ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
"corsheaders",
"corsheaders", # CORS Headers should be as high as possible.
"rest_framework",
# "quiz.apps.QuizConfig",
"django.contrib.admin",
......@@ -45,7 +45,7 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"corsheaders.middleware.CorsMiddleware", # CORS Middleware should be as high as possible.
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
......@@ -55,18 +55,7 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
# CORS_ALLOW_HEADERS = [
# "x-total-count",
# "accept-encoding",
# "authorization",
# "content-type",
# "dnt",
# "origin",
# "user-agent",
# "x-csrftoken",
# "x-requested-with",
# ]
# SOURCE: https://github.com/bmihelac/ra-data-django-rest-framework
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "quiz.pagination.PageNumberWithPageSizePagination",
"PAGE_SIZE": 10,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment