0

I am trying to paginate my results(dictionary values) to html page. While doing I am getting an error

unhashable type: 'slice'

views.py


from django.shortcuts import render

import urllib.request
import json
from django.core.paginator import Paginator

def display(request):
    cities=['vijayawada','guntur','tenali','rajahmundry','amaravathi','Bengaluru','Mangaluru','Chikkamagaluru','Chennai']
    data = {}
    for city in cities:
        source = urllib.request.urlopen(
            'https://api.openweathermap.org/data/2.5/weather?q=' + city + '&units=metric&appid=APIID').read()
        list_of_data = json.loads(source)

        data[city] = {
            "country_code": str(list_of_data['sys']['country']),
            "coordinates": {
                'latitude':  str(list_of_data['coord']['lat']),
                'longitude': str(list_of_data['coord']['lon'])
            },
            "temp": str(list_of_data['main']['temp']) + ' °C',
            "pressure": str(list_of_data['main']['pressure']),
            "humidity": str(list_of_data['main']['humidity']),
            'main': str(list_of_data['weather'][0]['main']),
            'description': str(list_of_data['weather'][0]['description']),
            'icon': list_of_data['weather'][0]['icon'],
        }
        p = Paginator(data,3)
        page = request.GET.get('page')
        d = p.get_page(page)
    context = {'data': d}
    return render(request, 'display.html', context)

Please help me to paginate the values in a dictionary

I expect the paginated results of my dictionary values

Vidya L
  • 13
  • 2

1 Answers1

0
from django.contrib.auth.models import User
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

user_list = User.objects.all()
page = request.GET.get('page', 1)

paginator = Paginator(user_list, 10)
try:
    users = paginator.page(page)
except PageNotAnInteger:
    users = paginator.page(1)
except EmptyPage:
    users = paginator.page(paginator.num_pages)

return render(request, 'core/user_list.html', { 'users': users })

this is how to u can use it.

Tanveer Ahmad
  • 706
  • 4
  • 12