chencourage 2019-11-02
目录
用于实现类似于django中 python3 manage.py runserver ...类似的命令
pip3 install flask-script
from flask_script import Manager app = Flask(__name__) manager=Manager(app) ... if __name__ == '__main__': manager.run() #以后在执行,直接:python3 manage.py runserver #python3 manage.py runserver --help
@manager.command def custom(arg): """ 自定义命令 python manage.py custom 123 :param arg: :return: """ print(arg) @manager.option('-n', '--name', dest='name') #@manager.option('-u', '--url', dest='url') def cmd(name, url): """ 自定义命令(-n也可以写成--name) 执行: python manage.py cmd -n lqz -u http://www.oldboyedu.com 执行: python manage.py cmd --name lqz --url http://www.oldboyedu.com :param name: :param url: :return: """ print(name, url) #有什么用? #把excel的数据导入数据库,定制个命令,去执行
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端口,会得到一个空白的页面
#在将表注册之前应该对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))