/'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 (将#修改为@)