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

Try Hand at Pagination

parent f0db3e3b
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"
...@@ -32,6 +32,8 @@ ALLOWED_HOSTS = [] ...@@ -32,6 +32,8 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
"corsheaders",
"rest_framework",
# "quiz.apps.QuizConfig", # "quiz.apps.QuizConfig",
"django.contrib.admin", "django.contrib.admin",
"django.contrib.auth", "django.contrib.auth",
...@@ -39,12 +41,11 @@ INSTALLED_APPS = [ ...@@ -39,12 +41,11 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"rest_framework",
"corsheaders",
"quiz", "quiz",
] ]
MIDDLEWARE = [ MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",
...@@ -52,9 +53,25 @@ MIDDLEWARE = [ ...@@ -52,9 +53,25 @@ MIDDLEWARE = [
"django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
] ]
# CORS_ALLOW_HEADERS = [
# "x-total-count",
# "accept-encoding",
# "authorization",
# "content-type",
# "dnt",
# "origin",
# "user-agent",
# "x-csrftoken",
# "x-requested-with",
# ]
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "quiz.pagination.PageNumberWithPageSizePagination",
"PAGE_SIZE": 10,
}
ROOT_URLCONF = "unisportomat.urls" ROOT_URLCONF = "unisportomat.urls"
TEMPLATES = [ TEMPLATES = [
......
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