2013-03-21 78 views
2

这里的瓶应用:Gunicorn 500'ing到烧瓶应用

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" 

工作正常,当我成为使用它瓶内置的开发服务器。

麻痹大脑告诉我,这只是一个简单的事情,我只是想念。 GUH。

回答

4

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

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

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

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

我认为值得指出的是,如果我有任何一种体面的伐木设置,我会立即抓住这个小小的麻烦。

+3

感谢您发布解决方案,我有完全相同的问题,并立即解决它。可能救了我几个小时。 – 2014-07-16 20:01:28