2013-03-21 79 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