Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create custom paginator in django

I’m trying to use django built-in paginator but it’s not working cuz I’m not using database storage object I’m creating a website with other api…

heres my code:

def index(request):
    response = requests.get("https://www.hetzner.com/a_hz_serverboerse/live_data.json")
    data = response.json()
    p = Paginator(data, 3)
    pn = request.GET.get('page')
    print(pn, p, data)
    page_obj = p.get_page(pn)

    context = {
        'data': data,
        'page_obj': page_obj
    }
    return render(request, 'index.html', context)

do i need to create custom paginator if so how should i? or is there solution?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

https://docs.djangoproject.com/en/4.0/topics/pagination/#example
You can give any list to the paginator.

The problem was that the response from the API did not return a list, but an object where the list was under the servers key.
Solution:


def index(request):
    response = requests.get("https://www.hetzner.com/a_hz_serverboerse/live_data.json")
    data = response.json()['server']
    p = Paginator(data, 3)
    pn = request.GET.get('page')
    print(pn, p, data)
    page_obj = p.get_page(pn)

    context = {
        'data': data,
        'page_obj': page_obj
    }
    return render(request, 'index.html', context)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading