M
M
Mimobike2021-08-14 12:18:10
Django
Mimobike, 2021-08-14 12:18:10

How to change pagination link?

hello, there is such a task -
GET method for getting all messages with pagination by 10 messages per request.
eg
/api/messages/list/0 will return first 10 messages
/api/messages/list/1 will return second 10 messages
etc

class MessagePagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'

i do pagination like this but it builds a link like /api/messages/?page=
how do i change to /api/messages/list/ and start pagination from 0 not 1

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-08-14
@fox_12

Take the base pagination class from django, extend it into your custom pagination class, then override the
get_next_link and get_previous_link
methods with your own. Next - use your custom class for pagination.

A
akrvch, 2021-08-22
@akrvch

You can do something like this:
urls.py

path(“api/messages/list/<int:page_number>“, views.your_view, name=“messages”)

views.py
def your_view(request, page_number)
    messages = Paginator(queryset, 10)
    return render(request, ‘template.html’, {‘messages’: get_page(page_number)})

template.html
<a href=“{% urlmessagesmessages.next_page_number %}”>next page</a>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question