Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
操纵python django字典内容_Python_Html_Json_Django - Fatal编程技术网

操纵python django字典内容

操纵python django字典内容,python,html,json,django,Python,Html,Json,Django,我正在使用Django开发一个简单的天气应用程序。我能够显示城市名称、温度、风速、条件描述以及添加/删除城市。添加城市后,web应用程序将显示一张带有该城市名称和上述所有天气信息的卡片 作为一名赛艇运动员,我想在城市卡上加上一个“safeToRow”部分。该部分将从python字典中获取城市的温度和风,并将其分配给两个变量。然后通过一些简单的if和else if条件来运行这些变量,以确定行是否安全,并最终返回safeToRow的最终值 Views.py def index(request):

我正在使用Django开发一个简单的天气应用程序。我能够显示城市名称、温度、风速、条件描述以及添加/删除城市。添加城市后,web应用程序将显示一张带有该城市名称和上述所有天气信息的卡片

作为一名赛艇运动员,我想在城市卡上加上一个“safeToRow”部分。该部分将从python字典中获取城市的温度和风,并将其分配给两个变量。然后通过一些简单的if和else if条件来运行这些变量,以确定行是否安全,并最终返回safeToRow的最终值

Views.py

def index(request):
    url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=APIKEY'


    if request.method == 'POST':
        form = CityForm(request.POST)
        form.save()

    form = CityForm()

    cities = City.objects.all()

    weather_data = []

    safeToRow = ""


    for city in cities:

        r = requests.get(url.format(city)).json()

        city_weather = {
            'city' : city.name,
            'temperature' : r['main']['temp'],
            'wind' : r['wind']['speed'],
            'description' : r['weather'][0]['description'],
            'icon' : r['weather'][0]['icon'],
        }

        weather_data.append(city_weather)


        temp = r['main']['temp']
        wind = r['wind']['speed']


        def row(temp, wind, safeToRow):
            if temp < 32:
                return safeToRow == "No"
            elif temp > 40 and wind < 15:
                return safeToRow == "No"
            elif temp < 40 and wind > 15:
                return safeToRow == "Yes"



    context = {'weather_data' : weather_data, 'form' : form, "safeToRow" : safeToRow}
    return render(request, 'weather/weather.html', context)
def索引(请求):
url='1〕http://api.openweathermap.org/data/2.5/weather?q={}&units=英制&appid=APIKEY'
如果request.method==“POST”:
表单=城市表单(request.POST)
form.save()
表单=城市表单()
cities=City.objects.all()
天气数据=[]
safeToRow=“”
对于城市中的城市:
r=requests.get(url.format(city)).json()
城市天气={
“city”:city.name,
“温度”:r['main']['temp'],
“风”:r[“风”][“速度”],
“说明”:r['weather'][0]['description'],
“图标”:r['weather'][0]['icon'],
}
天气数据。附加(城市天气)
温度=r['main']['temp']
风=r[“风”][“速度”]
def行(温度、风、保险箱箭头):
如果温度<32:
返回保险箱箭头==“否”
elif温度>40且风小于15:
返回保险箱箭头==“否”
elif温度<40且风>15:
return safeToRow==“是”
上下文={'weather_data':weather_data'form':form,“safeToRow”:safeToRow}
返回渲染(请求'weather/weather.html',上下文)
HTML部分

<div class="media-content">
                                <div class="content">
                                    <p>
                                        <span class="title">{{ city_weather.city }}</span>
                                        <br> 
                                        <span class="subtitle">{{ city_weather.temperature }}</span>
                                        <br> 
                                        <span class="subtitle">{{ city_weather.row }}</span>
                                        <br> 
                                        <span class="subtitle">{{ city_weather.wind }}</span>
                                        <br> {{ city_weather.description }}
                                    </p>
                                </div>
                            </div>


{{city_weather.city}

{{城市天气温度}
{{city_weather.row}
{{city_weather.wind}
{{city_weather.description}}


它在web应用程序的卡片上创建了一个部分,但该部分是空白的

您试图调用city_weather.row,但您将row定义为一个函数。

您有一个
row()
函数,但从未使用过它。在
上下文中
,您在
天气数据
下存储天气,但访问
{city_weather.whather}
在模板中。模板中是否有循环的天气数据?