Home > Blockchain >  How to create custom paginator in django
How to create custom paginator in django

Time:02-12

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?

CodePudding user response:

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)
  • Related