这是 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 的内置开发服务器提供服务时工作正常。
啊大脑告诉我,这是我只是想念的非常简单的东西。嗯。