我收到此错误: 当我尝试提交请求时,“请求的URL不允许使用该方法。”
这是我的python代码:
from flask import Flask,render_template,request
import requests
api_address='http://api.openweathermap.org/data/2.5/weather?appid=014093f04f1d04c9e512539a36d4aaa9&q='
app=Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/weather',methods=['POST'])
def weather():
city=request.form['city_name']
url=api_address + city
json_data=requests.get(url).json()
temp_k=float(json_data['main']['temp'])
temp_c=temp_k-273.15
return render_template("weather.html",temp=temp_c)
if __name__=='__main__':
app.run(debug=True)
以下是“ home.html”的HTML代码:
<!DOCTYPE html>
<html>
<body>
<p>
<form action='http://127.0.0.1:5000/' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>
“ weather.html”的HTML代码为:
<!DOCTYPE html>
<html>
<body>
<h1>Temperature: {{temp}} degree celcius</h1>
<h1>Condition: {{desc}}</h1>
</body>
</html>
我该怎么办?
答案 0 :(得分:0)
您要发布到http://127.0.0.1:5000/
而不是/weather
尝试一下:
<!DOCTYPE html>
<html>
<body>
<p>
<form action='/weather' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>