首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在烧瓶中提交请求时出现方法不允许错误

在烧瓶中提交请求时出现方法不允许错误
EN

Stack Overflow用户
提问于 2018-12-14 03:19:12
回答 1查看 38关注 0票数 -1

当我尝试提交一个请求时,我得到了这个错误:“请求的URL不允许该方法”。

下面是我的python代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
<!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代码是:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<body>
    <h1>Temperature: {{temp}} degree celcius</h1>
    <h1>Condition: {{desc}}</h1>
</body>
</html>

我该怎么办?

EN

回答 1

Stack Overflow用户

发布于 2018-12-14 05:05:41

您正在发布到http://127.0.0.1:5000/,而不是/weather

试试这个:

代码语言:javascript
复制
<!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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53768692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档