flask 项目的开发经验总结
=========================
目录结构
=========================
考虑到项目的扩展性, 采用 blueprint 进行组织. 假设 flaskapp 为根目录, 主要的程序放在 app 包中, 除了后台代码, 在app目录下还有templates/static/子目录. 为了重用, 最好的形式是一个建立 boilerplate 项目.
主要参考文档为:
https://github.com/mitsuhiko/flask/wiki/Large-app-how-to
http://www.realpython.com/blog/python/python-web-applications-with-flask-part-ii-app-creation
http://www.realpython.com/blog/python/rethink-flask-a-simple-todo-list-powered-by-flask-and-rethinkdb/
flaskapp
├── app
│ ├── __init__.py
│ ├── constants.py
│ ├── users[sub_app/module]
│ │ ├── constants.py
│ │ ├── decorators.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ └── views.py
│ ├── tickets[sub_app/module]
│ │ ├── constants.py
│ │ ├── decorators.py
│ │ ├── forms.py
│ │ ├── models.py
│ │ └── views.py
│ ├── templates
│ │ ├── forms
│ │ │ └── macros.html
│ │ ├── base.html
│ │ ├── index.html
│ │ ├── base_another.html
│ │ ├── 500.html (server error page)
│ │ ├── 404.html (not found page)
│ │ ├── method_not_allowed.html
│ │ ├── access_forbidden.html
│ │ ├── users
│ │ │ ├── profile.html
│ │ │ ├── login.html
│ │ │ └── register.html
│ │ └── tickets
│ │ ├── create.html
│ │ └── close.html
│ └── static
│ ├── favicon.ico
│ ├── img
│ ├── js
│ │ ├── main.js #our own js code
│ │ └── vendor
│ │ ├── bootstrap.min.js
│ │ └── jquery-1.7.2.min.js
│ └── css
│ ├── layout.less
│ ├── reset.less
│ └── vendor
│ └── bootstrap.css
├── flaskapp.db
├── config.py
├── requirements.txt
├── runserver.py
├── shell.py
├── tests 目录
└── docs 目录
----------------------
项目级的单元
----------------------
runserver.py 用来启动 web server, 从app包中进入flask app对象, 然后直接启动.
config.py 存储一些db的 connection 配置, 以及Flask SECRET_KEY 等等. 更多的配置项见 http://flask.pocoo.org/docs/config/
----------------------
应用级别的单元
----------------------
flaskapp/app/__init__.py, 作为整个app的入口, 做如下工作.
1.加载flask的config,
2.[如使用Flask-SQLAlchemy插件]创建 SqlAlchemy 的db 实例.
3.[如没使用Flask-SQLAlchemy插件]定义3个函数, 分别加上@app.before_request和@app.teardown_request和@app.after_request. before_request和teardown_request函数非常适合做创建和关闭db connection工作. after_request函数不适合用来关闭db connection, 因为after_request函数在有unhandled exception发生的情况下, 会被跳过. 而teardown 函数总是能保证被调用的.
4.注册sub app blueprint, 比如users和tickets
5.设置root url 和favicon.ico 的路由,
6.创建login_manager, 比如使用flask-login插件来创建一个login_manager
----------------------
子应用级别的单元,
----------------------
flaskapp/app/users/models.py, 和User相关的表模型
flaskapp/app/users/constants.py, 和User module相关的constant, 比如用户的激活状态, 用户的类型.
flaskapp/app/users/forms.py, 集中所有User module相关的表单类, 比如class LoginForm(Form) 和 class RegisterForm(Form) 类.
flaskapp/app/users/decorators.py, 和User module相关的一些decorator, 比如 requires_login, 供 views.py 使用.
flaskapp/app/users/views.py, 充当url路由角色(Flask是基于MVT模型, 这里的view相当于MVC模型中的Controller). 依据web请求类型和请求的url, 路由到指定的view函数, 在view函数中, 做逻辑处理, 然后, 或展现form, 或跳转到其他url.
----------------------
templates目录
----------------------
flaskapp/app/templates/base.html, 模板的模板, 根据需要, 可以设置多个base页面
flaskapp/app/templates/forms/macros.html, 定义一些宏, 供form页面调用, 用来渲染form的元素
flaskapp/app/templates/users/login.html, 在flaskapp/app/users/views.py应该有一个同名的view函数
----------------------
static目录
----------------------
flaskapp/app/static/favicon.ico, 16 × 16 pixels and in the ICO format
=========================
源码示例
=========================
------------------------------
runserver.py
------------------------------
runserver.py 用来启动 web server.
# -*- coding: utf-8 -*-
from __future__ import absolute_import from app import app app.run()
------------------------------
config.py
------------------------------
config.py 存储一些db的 connection 配置, 以及Flask SECRET_KEY 等等. 更多的配置项见 http://flask.pocoo.org/docs/config/
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import os
from datetime import timedelta
_basedir = os.path.abspath(os.path.dirname(__file__)) DEBUG = True SECRET_KEY = os.urandom(24)
PERMANENT_SESSION_LIFETIME=timedelta(seconds=24*60*60) CSRF_ENABLED = True
CSRF_SESSION_KEY = SECRET_KEY
------------------------------
app/__init__.py
------------------------------
flaskapp/app/__init__.py, 作为整个app的入口, 做如下工作.
1.加载flask的config,
2.[如使用Flask-SQLAlchemy插件]创建 SqlAlchemy 的db 实例.
3.[如没使用Flask-SQLAlchemy插件]定义3个函数,
分别加上@app.before_request和@app.teardown_request和@app.after_request.
before_request和teardown_request函数非常适合做创建和关闭db connection工作.
after_request函数不适合用来关闭db connection, 因为after_request函数在有unhandled
exception发生的情况下, 会被跳过. 而teardown 函数总是能保证被调用的.
4.注册sub app blueprint, 比如users和tickets
5.设置root url 和favicon.ico 的路由,
6.创建login_manager, 比如使用flask-login插件来创建一个login_manager
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from flask import Flask, g, render_template, send_from_directory
import os
import os.path
_basedir = os.path.abspath(os.path.dirname(__file__))
configPy=os.path.join(os.path.join( _basedir,os.path.pardir), 'config.py') app = Flask(__name__) # create our application object app.config.from_pyfile(configPy)
#app.debug=True #change some attribute after load configuration flask_sqlalchemy_used=True # 如果使用Flask-SQLAlchemy了
db = SQLAlchemy(app) #create a db (SQLAlchemy) object from our app object login_manager = LoginManager(app) #create a LoginManager Object from our app object
#add our view as the login view to finish configuring the LoginManager
login_manager.login_view = "users.login_view" #register the users module blueprint
from app.users.views import mod as usersModule
app.register_blueprint(usersModule) #register the tickets module blueprint
from app.tickets.views import mod as ticketsModule
app.register_blueprint(ticketsModule) def connect_db(): # 如果没使用Flask-SQLAlchemy
if not flask_sqlalchemy_used:
return sqlite3.connect('/path/to/database.db')
else:
return None @app.before_request
def before_request():
"""Make sure we are connected to the database each request."""
if not flask_sqlalchemy_used:
g.db = connect_db() @app.teardown_request
def teardown_request(response):
"""Closes the database again at the end of the request."""
if not flask_sqlalchemy_used:
g.db.close()
return response #*****************
# controllers
#***************** @app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'ico/favicon.ico') @app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404 @app.route("/")
def index():
return render_template('index.html')
------------------------------
app/users/views.py.py
------------------------------
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from flask import Blueprint, render_template, flash, redirect, session, url_for, request, g
from flask.ext.login import login_user, logout_user, login_required
from app import app, db, login_manager
from forms import LoginForm, RegistrationForm
from app.users.models import User mod = Blueprint('users', __name__) #register the users blueprint module @login_manager.user_loader
def load_user(user_id):
return User.query.get(user_id) @mod.route('/login/', methods=('GET', 'POST'))
def login_view():
form = LoginForm(request.form)
if form.validate_on_submit():
user = form.get_user()
login_user(user)
flash("Logged in successfully.")
return redirect(request.args.get("next") or url_for("index"))
return render_template('users/login.html', form=form) @mod.route('/register/', methods=('GET', 'POST'))
def register_view():
form = RegistrationForm(request.form)
if form.validate_on_submit():
user = User()
form.populate_obj(user)
db.session.add(user) #使用SqlAlchemy保存
db.session.commit()
login_user(user)
return redirect(url_for('index'))
return render_template('users/register.html', form=form) @login_required
@mod.route('/logout/')
def logout_view():
logout_user()
return redirect(url_for('index'))
macros.html, 是个jinja2的宏文件, 我们在该文件中可定义一些宏, 供form页面调用, 用来渲染form的元素. 用法是, 在我们的html文件中, 引入这个宏文件即可.
用法:
{% from "macros.html" import form_field %}
macros.html内容, 可以自动将form兼容 bootstrap. 内容摘自 https://gist.github.com/rawrgulmuffins/6025599
{% macro form_field(field) -%}
{% set with_label = kwargs.pop('with_label', False) %}
{% set placeholder = '' %}
{% if not with_label %}
{% set placeholder = field.label.text %}
{% endif %}
<div class="control-group {% if field.errors %}error{% endif %}">
{% if with_label %}
<label for="{{ field.id }}" class="control-label">
{{ field.label.text }}{% if field.flags.required %} *{% endif %}:
</label>
{% endif %}
<div class="controls">
{% set class_ = kwargs.pop('class_', '') %}
{% if field.flags.required %}
{% set class_ = class_ + ' required' %}
{% endif %}
{% if field.type == 'BooleanField' %}
<label class="checkbox">
{{ field(class_=class_, **kwargs) }}
{{ field.label.text|safe }}
</label>
{% else %}
{% if field.type in ('TextField', 'TextAreaField', 'PasswordField') %}
{% set class_ = class_ + ' input-xlarge' %}
{% elif field.type == 'FileField' %}
{% set class_ = class_ + ' input-file' %}
{% endif %}
{% if field.type == 'SelectField' %}
{{ field(class_=class_, **kwargs) }}
{% else %}
{{ field(class_=class_, placeholder=placeholder, **kwargs) }}
{% endif %}
{% endif %}
{% if field.errors %}
<span class="error help-inline">{{ field.errors|join(', ') }}</span>
{% endif %}
{% if field.description %}
<p class="help-block">{{ field.description|safe }}</p>
{% endif %}
</div>
</div>
{%- endmacro %}
flask 项目的开发经验总结的更多相关文章
- Python框架 Flask 项目实战教程
本文目的是为了完成一个项目用到的flask基本知识,例子会逐渐加深.最好对着源码,一步一步走.下载源码,运行pip install -r requirements.txt 建立环境python db_ ...
- 通过VM虚拟机安装Ubuntu server部署flask项目
1. VM安装Ubuntu server 14.04,系统安装完成后,首先安装pip工具方便之后的包安装,此处需先使用 apt-get install update,apt-get install u ...
- flask项目开发中,遇到http 413错误
在flask项目中,上传文件时后台报http 413 Request Entity Too Large 请求体太大错误! 解决的2种方法: 1.在flask配置中设置 MAX_CONTENT_LENG ...
- flask项目部署到阿里云 ubuntu16.04
title: flask项目部署到阿里云 ubuntu16.04 date: 2018.3.6 项目地址: 我的博客 部署思路参考: Flask Web开发>的个人部署版本,包含学习笔记. 开始 ...
- 部署Flask项目到腾讯云服务器CentOS7
部署Flask项目到腾讯云服务器CentOS7 安装git yum install git 安装依赖包 支持SSL传输协议 解压功能 C语言解析XML文档的 安装gdbm数据库 实现自动补全功能 sq ...
- pycharm创建Flask项目,jinja自动补全,flask智能提示
pycharm创建Flask项目,jinja自动补全,flask智能提示 之前一直都是用在idea里创建空项目然后导入,之后就没有各种的智能提示,在选择文类,选择模板之类的地方就会很麻烦. 步骤1:用 ...
- flask 项目基本框架的搭建
综合案例:学生成绩管理项目搭建 一 新建项目目录students,并创建虚拟环境 mkvirtualenv students 二 安装开发中使用的依赖模块 pip install flask==0.1 ...
- windows环境隐藏命令行窗口运行Flask项目
Linux下可以使用nohub来使Flask项目在后台运行,而windows环境下没有nohub命令,如何让Flask项目在windows中在后台运行而不显示命令行窗口呢? 1.写一个.bat脚本来启 ...
- nginx + gunicorn + flask项目发布
程序安装(linux mint) gunicorn安装:pip install gunicorn nginx安装:sudo apt-get install nginx 配置 nginx默认配置信息在/ ...
随机推荐
- wpf配置菜单栏
WPF 内建了两种菜单——Menu 和ContextMenu(上下文菜单). 1. Menu Menu 的项可以是任何东西,但是你应该使用MenuItem 以及Separator 对象. <Me ...
- DedeCMS顽固木马后门专杀工具V2.0实现方式研究
catalog . 安装及使用方式 . 检查DEDECMS是否为最新版本 . 检查默认安装(install)目录是否存在 . 检查默认后台目录(dede)是否存在 . 检查DedeCMS会员中心是否关 ...
- Cloud Design Patterns Book Reading(undone)
目录 . the most common problem areas in cloud application development ) Availability ) Data Management ...
- ANDROID版本号和版本名称的重要性介绍
当我们在刚开始学习ANDROID的时候,可能不会过多的关注这个位于manifest.xml文件中的versionCode和versionName. 但是其实一个好的版本控制,对于我们有至关重要的作用. ...
- django-redis和redis-py
项目之前使用memcache做缓存,现在转到redis,改写几个语句的事情,然后就这种我把django-redis和py-redis搞混了,记录一下. django默认使用memcache做缓存,这里 ...
- CentOS terminal 安装 matlab(mode=silent)
1. 下载matlab for Unix 2014 ,需要crack文件 2. 挂载iso文件, mount -o loop,ro Mathworks.Matlab.R2014a.iso /media ...
- mysql 生成排名字段
假设有test表,下图为表机构和数据,score表示积分.现在要查询积分排名为第几的id?? 查询语句 select id,score,(@rowno:=@rowno+1) as rowno from ...
- 捉襟见肘之gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer
-- :::60b] *** -[ZFModalTransitionAnimator gestureRecognizer:shouldBeRequiredToFailByGestureRecogniz ...
- HD1814Peaceful Commission(模板题)
题目链接 题意: 和平委员会 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条 ...
- css014 响应式web设计
css014 响应式web设计 一. 响应式web设计基础知识 1.rwd的三大理念:a.用于布局的弹性网络, b.用于图片和视频的弹性媒体,c.为不同屏幕宽度创建的不同样式的css媒体查询. ...