一.多APP 二.flask admin 安裝 簡單使用 將表模型註冊到admin中 如果有個欄位是圖片指端 ...
一.多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"
# http://localhost:5000/index
# http:/localhost:5000/sec/index2
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__)
#將app註冊到adminzhong
admin = Admin(app)
if __name__=="mian":
app.run()
#訪問
#127.0.0.1:5000/admin埠,會得到一個空白的頁面
將表模型註冊到admin中
#在將表註冊之前應該對app進行配置
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
#導入models文件的中的表模型
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
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='文件'))
#如果有個欄位要是上傳文件重寫該方法的modleView類,假設imgae_url是文件圖片的欄位
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))