【问题标题】:Gunicorn 500'ing on POST to Flask appGunicorn 500'ing on POST to Flask app
【发布时间】:2013-03-21 21:15:39
【问题描述】:

这是 Flask 应用程序:

from flask import Flask, request, render_template, redirect, url_for, flash
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/contact', methods = ['POST'])
def contact():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        message = request.form['message']

        flash(name)
        flash(email)
        flash(message)

    return redirect(url_for('index'))

if __name__ == '__main__':
    app.debug = True
    app.secret_key='a;sldfjka;oeiga;lbneas; biaweag'
    app.run()

这是我正在使用的模板:

<!DOCTYPE html>
<html>
    <head>
    </head>

    <body> 
        <form action="/contact" method="post">
            <input type="text" name="name" />
            <input type="text" name="email" />
            <textarea name="message"> </textarea>
            <input type="submit" />
        </form> 
        <ul>
        {% for message in get_flashed_messages() %}
            <li><h3> {{ message }} </h3></li>
        {% endfor %}
        </ul>
    </body>
</html>

这是我用来服务它的gunicorn 命令行:

gunicorn --error-logfile err.log --access-logfile access.log \
--log-level debug -b 127.0.0.1:5000 --debug -w 4 wsgi:app

它可以很好地为模板提供 GET 请求。但是,当我实际发布表单时,它已经 500 了。这是响应标头:

HTTP/1.1 500 INTERNAL SERVER ERROR
Server: gunicorn/0.17.2
Date: Thu, 21 Mar 2013 21:57:25 GMT
Connection: close
Content-Type: text/html
Content-Length: 291

gunicorn 日志中显示的内容如下:

==> err.log <==
2013-03-21 17:12:38 [10092] [DEBUG] POST /contact

==> access.log <==
"127.0.0.1 - - [21/Mar/2013:17:12:38] "POST /contact HTTP/1.1" 500 291 \
"http://localhost:5000/" "Mozilla/5.0 (X11; Linux x86_64; rv:19.0) \
Gecko/20100101 Firefox/19.0"

当我使用 Flask 的内置开发服务器提供服务时工作正常。

Mah 大脑告诉我,这是一件非常简单的事情,我只是想念它。呸。

【问题讨论】:

    标签: python-2.7 flask virtualenv gunicorn


    【解决方案1】:

    我在 name == main 块中定义了 secret_key。 Gunicorn 感到窒息,因为该应用程序没有说它需要处理表单提交的密钥。

    if __name__ == '__main__':
        app.secret_key = #...
    

    只要在 gunicorn 运行脚本时对它进行评估,该行就需要在其他任何地方。

    app = Flask(__name__)
    app.secret_key = #...
    

    我认为值得指出的是,如果我有任何不错的日志记录设置,我会立即发现这个小问题。

    【讨论】:

    • 感谢您费心发布解决方案,我遇到了完全相同的问题,这立即解决了。可能节省了我几个小时。
    猜你喜欢
    • 1970-01-01
    • 2021-06-24
    • 2023-03-20
    • 2020-06-09
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    • 2022-11-20
    相关资源
    最近更新 更多