创建项目 https://cn.vuejs.org/guide/scaling-up/tooling.html
建议增加的插件
创建玩之后,修改一下 vite.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 export default defineConfig ({ build : { outDir : "../static" , emptyOutDir : true , }, server : { proxy : { "/api" : { target : "http://localhost:7001" , changeOrigin : true , }, }, }, });
Flask 需要加上两个路由
1 2 3 4 5 6 7 8 9 10 11 from flask import send_from_directoryapp = flask.Flask(__name__) @app.route("/" ) def index (): return send_from_directory("static" , "index.html" ) @app.route("/<path:path>" ) def static_file (path: str ): return send_from_directory("static" , path)
这样子访问 Index 主页就可以直接加载 vue 编译出来的文件了
数据库 一般推荐的都是 SQLAlchemy
下面是个简单的例子通过DB_URL环境变量传递要连接的地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTimefrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import sessionmakerfrom datetime import datetimeimport osdb_url = os.getenv("DB_URL" ) if not db_url: raise RuntimeError("DB_URL not set" ) engine = create_engine(db_url) Base = declarative_base() Session = sessionmaker(bind=engine) def get_session (): session = Session() return session class User (Base ): __tablename__ = 'user' id = Column(Integer, primary_key=True ) gmt_create = Column(DateTime, default=datetime.now) gmt_modified = Column(DateTime, default=datetime.now, onupdate=datetime.now) email = Column(String(50 ), unique=True , nullable=False ) password = Column(String(50 )) admin = Column(Boolean)
常用链接