Gunicorn 500'在POST到Flask应用程序

not*_*you 2 virtualenv flask python-2.7 gunicorn

这是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()
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的模板:

<!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>
Run Code Online (Sandbox Code Playgroud)

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

gunicorn --error-logfile err.log --access-logfile access.log \
--log-level debug -b 127.0.0.1:5000 --debug -w 4 wsgi:app
Run Code Online (Sandbox Code Playgroud)

它提供模板的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
Run Code Online (Sandbox Code Playgroud)

以下是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"
Run Code Online (Sandbox Code Playgroud)

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

Mah大脑告诉我,这只是一种简单易懂的东西,我只是想念它.GUH.

not*_*you 5

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

if __name__ == '__main__':
    app.secret_key = #...
Run Code Online (Sandbox Code Playgroud)

该行必须在其他任何地方,只要在脚本由gunicorn运行时进行评估.

app = Flask(__name__)
app.secret_key = #...
Run Code Online (Sandbox Code Playgroud)

我认为值得指出的是,如果我有任何像样的日志记录设置,我会立即抓住这个小螺丝.

  • 感谢您无法发布解决方案,我遇到了完全相同的问题,并立即解决了这个问题.可能节省了我几个小时. (3认同)