在 djnago 中对字典值的结果进行分页时,我收到一个不可哈希类型的错误:'slice'



我试图将我的结果(字典值)分页到html页面。当我这样做我得到一个错误

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)

请帮我对字典中的值进行分页

我期望我的字典值的分页结果

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 })

这是你如何使用它。

相关内容

  • 没有找到相关文章

最新更新