在Django中的API URL中传递参数

问题描述 投票:0回答:1

我正在玩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

api django-views
1个回答
0
投票

您需要将已保存的数据传递到视图中

context = {'weather' : weather }
return render(request, 'weather/forecast.html', context=context)

然后,您可以访问html脚本中context变量中的数据

<div>
{{ weather }}
</div>
© www.soinside.com 2019 - 2024. All rights reserved.