一.多APP
from werkzeug.wsgi import DispatcherMiddleware
from werkzeug.serving import run_simple
from flask import Flask, current_app
app1 = Flask('app01')
app2 = Flask('app02')
@app1.route('/index')
def index():
return "app01"
@app2.route('/index2')
def index2():
return "app2"
dm = DispatcherMiddleware(app1, {
'/sec': app2,
})
if __name__ == "__main__":
run_simple('localhost', 5000, dm)
二.flask-admin
安装
pip3 install flask_admin
简单使用
from flask import Flask
from flask_admin import Admin
app = Flask(__name__)
admin = Admin(app)
if __name__=="mian":
app.run()
将表模型注册到admin中
SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:@127.0.0.1:3307/py9api?charset=utf8mb4"
SQLALCHEMY_POOL_SIZE = 5
SQLALCHEMY_POOL_TIMEOUT = 30
SQLALCHEMY_POOL_RECYCLE = -1
from flask_admin.contrib.sqla import ModelView
from api.models import Stock,Product,Images,Category,Wxuser,Banner
admin.add_view(ModelView(Stock, db.session))
admin.add_view(ModelView(Product, db.session))
admin.add_view(ModelView(Category, db.session))
如果有个字段是图片指端
from flask_admin.contrib.fileadmin import FileAdmin,form
file_path = op.join(op.dirname(__file__), 'static')
admin = Admin(app)
admin.add_view(FileAdmin(file_path, '/static/', name='文件'))
class ImagesView(ModelView):
form_extra_fields = {
'image_url': form.ImageUploadField('Image',
base_path=file_path,
relative_path='uploadFile/'
)
}
admin.add_view(ImagesView(Images, db.session))