使用pipenv创建虚拟环境
`pip install pipenv`
创建项目目录
`mkdir order`
为项目order创建虚拟环境
`cd order``pipenv install ``# 创建虚拟环境`
激活虚拟环境
`pipenv shell`
安装和卸载包
`pipenv install flask ``# 安装包``pipenv uinstall flask ``# 卸载包`
查看当前虚拟环境位置
`pipenv ``-``-``venv`
退出虚拟环境
`exit`
初始化环境安装依赖
`flask``flask``-``sqlalchemy``mysqlclient``flask_script `
创建项目根目录 order
在order目录下创建包config用来管理配置文件
通用配置文件:
# -*- coding: utf-8 -*-
SERVER_PORT = 5000
DEBUG = False
SQLALCHEMY_ECHO = False
本地配置文件:
# -*- coding: utf-8 -*-
DEBUG = True
SQLALCHEMY_ECHO = True
SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]/mysql?charset=utf8mb4'
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENCODING = "utf8mb4"
生产配置文件:
# -*- coding: utf-8 -*-
from flask import Flask
from flask_script import Manager
from flask_sqlalchemy import SQLAlchemy
import os
class Application( Flask ):
def __init__(self,import_name):
super( Application,self ).__init__( import_name )
self.config.from_pyfile( 'config/base_setting.py' )
if "ops_config" in os.environ:
self.config.from_pyfile( 'config/%s_setting.py'%os.environ['ops_config'] )
db.init_app( self )
db = SQLAlchemy()
app = Application( __name__ )
manager = Manager( app )
在order目录下创建www.py 封装HTTP相关初始化
# -*- coding: utf-8 -*-
from application import app
from web.controllers.index import route_index
app.register_blueprint( route_index,url_prefix = "/" )
在order目录下创建manager.py 启动入口
# -*- coding: utf-8 -*-
from application import app,manager
from flask_script import Server
import www
##web server
manager.add_command( "runserver", Server( host='0.0.0.0',port=app.config['SERVER_PORT'],use_debugger = True ,use_reloader = True) )
def main():
manager.run()
if __name__ == '__main__':
try:
import sys
sys.exit( main() )
except Exception as e:
import traceback
traceback.print_exc()
在order目录下创建readme.md
Python Flask订餐系统
=====================
##启动
* export ops_config=local|production && python manage.py runserver
##flask-sqlacodegen
flask-sqlacodegen 'mysql://root:[email protected]/food_db' --outfile "common/models/model.py" --flask
flask-sqlacodegen 'mysql://root:[email protected]/food_db' --tables user --outfile "common/models/user.py" --flask
## 所见即所得编辑器ueditor
UE.getEditor('editor',{
toolbars: [
[ 'undo', 'redo', '|',
'bold', 'italic', 'underline', 'strikethrough', 'removeformat', 'formatmatch', 'autotypeset', 'blockquote', 'pasteplain', '|', 'forecolor', 'backcolor', 'insertorderedlist', 'insertunorderedlist', 'selectall', '|','rowspacingtop', 'rowspacingbottom', 'lineheight'],
[ 'customstyle', 'paragraph', 'fontfamily', 'fontsize', '|',
'directionalityltr', 'directionalityrtl', 'indent', '|',
'justifyleft', 'justifycenter', 'justifyright', 'justifyjustify', '|', 'touppercase', 'tolowercase', '|',
'link', 'unlink'],
[ 'imagenone', 'imageleft', 'imageright', 'imagecenter', '|',
'insertimage', 'insertvideo', '|',
'horizontal', 'spechars','|','inserttable', 'deletetable', 'insertparagraphbeforetable', 'insertrow', 'deleterow', 'insertcol', 'deletecol', 'mergecells', 'mergeright', 'mergedown', 'splittocells', 'splittorows', 'splittocols' ]
],
enableAutoSave:true,
saveInterval:60000,
elementPathEnabled:false,
zIndex:4,
serverUrl:common_ops.buildUrl( '/upload/ueditor' )
});
##可参考资料
* [python-Flask(jinja2)语法:过滤器](https://www.jianshu.com/p/3127ac233518)
* [SQLAlchemy 各种查询语句写法](https://wxnacy.com/2017/08/14/python-2017-08-14-sqlalchemy-filter/)
在order目录下创建requirements.txt
flask
flask-sqlalchemy
mysqlclient
flask_script
在order目录下创建common包 存放公用部分
order/common/init.py
在common下创建 libs (共用方法,类), models(数据模型)
order/common/libs/init.py
order/common/models/init.py
在order目录下创建doc目录 存放存放文档
order/doc/mysql.md
在order目录下创建jobs包 用来存放一些定时的任务
order/jobs/init.py
order/jobs/task/init.py
在 order 目录下创建web包用来存放蓝图
web/init.py
web/controllers/index.py
from flask import Blueprint
route_index = Blueprint( 'index_page',__name__ )
@route_index.route("/")
def index():
return 'hello'
web/controllers/init.py
在终端添加环境变量
export ops_config=local
启动项目
python manage.py runserver
输入测试url
http://127.0.0.1:5000/