【问题标题】:method not allowed error when submitting request in flask在烧瓶中提交请求时方法不允许错误
【发布时间】:2018-12-14 05:05:40
【问题描述】:

我收到此错误: 当我尝试提交请求时,“请求的 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>

我该怎么办???

【问题讨论】:

标签: python html flask


【解决方案1】:

你发帖到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>

【讨论】:

    猜你喜欢
    • 2020-07-30
    • 2023-03-09
    • 2014-03-08
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多