【问题标题】:My app.py is not able to recive the request at the backend and getting 404 error我的 app.py 无法在后端接收请求并收到 404 错误
【发布时间】:2019-05-15 07:07:41
【问题描述】:

我的 app.py 是

app=flask.Flask(__name__,template_folder='templates')
@app.route('/')

def main():
 if flask.request.method == 'GET':
    return(flask.render_template('main.html'))
 if flask.request.method == 'POST':
    news = flask.request.form['article']
    input_variables = pd.DataFrame([[news]],
                                   columns=['news'],
                                   dtype=object)
    prediction = model.predict(input_variables)[0]
    return flask.render_template('main.html',
                                 original_input={'news':news},
                                 result=prediction,
                                 )
if __name__=='__main__':
 app.run()

<!doctype html>
<html>
<style>
form {
    margin: auto;
    width: 35%;
}
.result {
    margin: auto;
    width: 35%;
    border: 1px solid #ccc;
}
</style>
<head>
    <title>News Classification Model</title>
</head>
<form action="{{ url_for('main') }}" method="POST">
    <fieldset>
        <legend>Input values:</legend>
        Enter any news:
        <input name="news" type="object" required>
        <br>
        <br>
        <input type="submit">
    </fieldset>
</form>
<br>
<div class="result" align="center">
    {% if result %}
        {% for variable, value in original_input.items() %}
            <b>{{ variable }}</b> : {{ value }}
        {% endfor %}
        <br>
        <br> Predicted Class of the news :
           <p style="font-size:50px">{{ result }}</p>
    {% endif %}
</div>
</html>
我认为我在 html 文件中遇到了问题,因此任何帮助都会很好地纠正它。 我是 Flask 的新手,所以我无法解决它。我的 main.html 文件是从某人那里获取的,这就是为什么我想我遇到了这个问题。

【问题讨论】:

  • 404 错误意味着请求甚至没有到达烧瓶应用程序。忘记 html,检查你的服务器配置,即 nginx/apache、gunicorn 和/或你在后端使用的任何东西来运行这个应用程序。如果它是 Amazon EC2 实例,还要检查您的防火墙。
  • 你能展示一下你的项目的结构吗?您的 main.html 在哪个文件夹中?你确定把它放在 templates 文件夹里吗?

标签: python post flask get


【解决方案1】:

你需要删除这两行之间的空行:

@app.route('/')

def main():

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2012-05-28
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2013-05-22
    • 1970-01-01
    相关资源
    最近更新 更多