在Django的API URL中传递参数

时间:2018-09-17 20:50:30

标签: api django-views

我正在使用API​​,这是我的第一个API视图 请帮我了解我哪里错了。

我使用此API     api.openweathermap.org/data/2.5/weather?q= {城市名称},{国家代码} 我写了这个观点:

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')

这是非常基本的视图,例如我的第一张照片(“ Hello,World!”),但id根本不起作用))   如果您向我建议一些有关此的文章,我将不胜感激。  在这里http://docs.python-requests.org/en/latest/user/quickstart/#json-response-content

找不到答案

1 个答案:

答案 0 :(得分:0)

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

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

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

<div>
{{ weather }}
</div>
相关问题