【发布时间】:2018-10-27 01:07:08
【问题描述】:
我知道这可能是一个非常愚蠢的问题,但我是 django 的新手,现在尝试解决这个问题几个小时。 我想将一个变量从一个 html 表单传递到后端,然后在那里使用该变量来发出一个 api 请求。 然后我想将 api-request 的结果传回 index.html 页面。
例如:
index.html
<form action="#" method="post">
{% csrf_token %}
<input type="text" class="form-control" id="city" placeholder="" value="">
<input type="submit" value="Submit">
</form>
forms.py
import requests
api_address='http://api.openweathermap.org/data/2.5/weather?
appid=KEY&q='
city = FORM-VARIABLE
url = api_address + city
json_data = requests.get(url).json()
kelvin = json_data['main']['temp']
temperature = round(kelvin - 273.15,0)
然后在index.html中显示温度
【问题讨论】:
-
您的输入必须具有
name属性。之后可以看这个问题:stackoverflow.com/questions/4706255/…