微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

/'ModelBase'对象的TypeError不可迭代...如何在Django中解决此问题

如何解决/'ModelBase'对象的TypeError不可迭代...如何在Django中解决此问题

/'ModelBase'对象处的TypeError不可迭代...如何在Django中解决此问题?

这是我的模型。py:

from django.db import models

class City(models.Model):
    name = models.CharField(max_length=30)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = 'cities'

views.py:

import requests
from django.shortcuts import render
from . models import City

def home(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=37dcd1bbdc2216925ebb043a870105a6'
    cities = City.objects.all()
    weather_data = []
    for city in City:
        city_weather = requests.get(url.format(city)).json()
        weather = {
            'city' : city,'temperature' : city_weather['main']['temp'],'description' : city_weather['weather'][0]['description'],'icon' : city_weather['weather'][0]['icon']
        }
    weather_data.append(weather)
    context = {'weather_data' : weather_data}
    return render(request,'home.html',context)

由于我是Django的初学者,请帮助我解决这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。