文档

Miguel Grinberg博客内容很多, 上面的The Flask Mega-Tutorial就是其博客上的在线教程, Flask Web开发是他出的书, 有中文版.

Flask-SQLAlchemy

逆序排序desc:

Blog.query.order_by(Blog.date.desc()).all()

或者:

Blog.query.order_by(db.desc(Blog.date)).all()

部署

Nginx

server {
    listen       80;
    server_name  localhost;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/uwsgi.sock;
    }
}

或者参考Flask - uWSGI文档:

location / { try_files $uri @yourapplication; }
location @yourapplication {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/uwsgi.sock;
}

yourapplication改为实际的包或模块名.

这里使用了location的@语法, 参考nginx文档

The prefix "@" specifies a named location. Such locations are not used during normal processing of requests, they are intended only to process internally redirected requests (see error_page, try_files).

uWSGI

安装uwsgi

执行命令:

uwsgi -s /tmp/uwsgi.sock --chmod-sock=666 --pythonpath /path/to/project/root --module yourmodulename --callable app

其中:

如果使用了virtualenv, 当没有active这个virtualenv时, 需要用H|--home|--virtualenv|--venv|--pyhome指定virtualenv的路径

也可以写成配置文件, 如ini, xml等:

$ cat uwsgi_example.ini
[uwsgi]
# application's base folder
base =

# python module to import
app = blogwall
module = %(app)

# set PYTHONHOME/virtualenv
# if use virtual, set this option
#home =
# add directory (or glob) to pythonpath
pythonpath = %(base)

# socket file's location
socket =

# permissions for the socket file
chmod-socket = 666

# the variable that holds a flask application inside the module imported at line #6
callable = app

# location of log files
logto =

详细可参考uWSGI配置文档

其它一些参考文档:

FAQ

运行Flask自带的example flaskr, 提示:

AttributeError: 'Flask' object has no attribute 'cli'

原因是flask命令行方式在dev版才支持, issue