我正在玩API,这是我的第一个API View,请帮我理解我错在哪里。
我使用这个API api.openweathermap.org/data/2.5/weather?q={city name},{country code},我写了这个观点:
def forecast(request):
url = 'http://api.openweathermap.org/data/2.5/weather?q= {}&appid=My_Key'
cities = {'city': 'London', 'cod' : 826}
city_weather = requests.get(url.format(cities)).json()
weather = {
'temperature': city_weather['main']['temp'],# one parameter, just to check whethe it works
}
context = {'weather' : weather }
return render(request, 'weather/forecast.html')
这是非常基本的视图,比如我的第一个印刷品(“你好,世界!”),但是ID根本不起作用))如果你告诉我一些关于此的文章,我将感激不尽。在这里找不到答案http://docs.python-requests.org/en/latest/user/quickstart/#json-response-content
您需要将已保存的数据传递到视图中
context = {'weather' : weather }
return render(request, 'weather/forecast.html', context=context)
然后,您可以访问html脚本中context
变量中的数据
<div>
{{ weather }}
</div>