如何創(chuàng)建一個(gè)Flask項(xiàng)目并進(jìn)行簡(jiǎn)單配置
安裝的依賴包
flask pymysql flask_script flask_migrate flask_sqlalchemy
創(chuàng)建Flask項(xiàng)目(項(xiàng)目目錄結(jié)構(gòu))
flaskexample |---static |---templates |---app.py
在項(xiàng)目下創(chuàng)建settings文件,進(jìn)行配置
setting.py class DevelopmentConfig: DEBUG = True SQLALCHEMY_DATABASE_URI=mysql+pymysql://用戶:密碼@host/數(shù)據(jù)庫 SQLALCHEMY_TRACK_MODIFICATIONS=False SQLALCHEMY_ECHO=True ENV='development' # 此時(shí)項(xiàng)目目錄結(jié)構(gòu) flaskexample |---static |---templates |---app.py |---setting.py
在項(xiàng)目下創(chuàng)建apps文件,在__init__.py文件下進(jìn)行配置
apps.__init__.py from flask import Flask import setting from exts import db def create_app(): app = Flask(__name__, template_folser='../templates', static_folder='../static') app.config.from_object(setting.DevelopmentConfig) # 初始化ORM db.init_app(app=app) return app # 此時(shí)項(xiàng)目目錄結(jié)構(gòu) flaskexample |---apps |---__init__.py |---static |---templates |---app.py |---setting.py
在項(xiàng)目文件夾下的app.py中進(jìn)行配置
app.py from flask import Flask from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from apps import create_app from exts import db from apps.user import user app = create_app() # 配置manage manager = Manager(app=app) # 配置migrate命令,將相關(guān)命令交給manage migrate = Migrate(app=app, db=db) manager.add_command('db', MigrateCommand) # 注冊(cè)藍(lán)圖 app.register_blueprint(user) if __name__ == '__main__': manager.run()
創(chuàng)建exts文件,進(jìn)行ORM配置
exts.__init__.py from flask_sqlalchemy import SQLAlchemy # 配置ORM db = SQLAlchemy() # 此時(shí)項(xiàng)目目錄結(jié)構(gòu) flaskexample |---apps |---__init__.py |---exts |---__init__.py |---static |---templates |---app.py |---setting.py
在apps._init_.py中初始化ORM配置(已在文件中進(jìn)行配置)
在app.py中注冊(cè)migrate命令相關(guān)配置(已在文件中進(jìn)行配置)
在apps下創(chuàng)建app文件并創(chuàng)建相關(guān)文件(如用戶app)
# 此時(shí)項(xiàng)目目錄結(jié)構(gòu) flaskexample |---apps |---__init__.py |---user |---__init__.py |---view.py |---models.py |---exts |---__init__.py |---static |---templates |---app.py |---setting.py
在apps.user.models.py中編寫數(shù)據(jù)表的相關(guān)信息
user.model from exts import db class User(db.Model): username = db.Column(db.String(60), nullable=True) password = db.Column(db.String(60), nullable=True) ···
進(jìn)行數(shù)據(jù)遷移
# 1. 初始化表 python app.py db init # 2. 生成遷移文件 Python app.py db migrate # 3. 進(jìn)行數(shù)據(jù)遷移 python app.py db upgrate #當(dāng)命令2執(zhí)行成功后自動(dòng)生成migrations文件夾 # 此時(shí)項(xiàng)目目錄結(jié)構(gòu) flaskexample |---apps |---user |---__init__.py |---models.py |---view.py |---__init__.py |---exts |---__init__.py |---migrations |---versions |---9eef46471b52_.py |---alembic.ini |---env.py |---READEME |---script.py.mako |---static |---templates |---app.py |---setting.py
在user _init_下創(chuàng)建藍(lán)圖,并在apps的_init_下注冊(cè)
user.__init__ from flask import Blueprint user = Blueprint("user", __name__)
之后就可以在user下的view.py中進(jìn)行邏輯的處理了
以上就是如何創(chuàng)建一個(gè)Flask項(xiàng)目并進(jìn)行簡(jiǎn)單配置的詳細(xì)內(nèi)容,更多關(guān)于創(chuàng)建 flask項(xiàng)目的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 統(tǒng)計(jì)列表中不同元素的數(shù)量方法
今天小編就為大家分享一篇python 統(tǒng)計(jì)列表中不同元素的數(shù)量方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06聊聊Python中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問題
這篇文章主要介紹了聊聊Python中的浮點(diǎn)數(shù)運(yùn)算不準(zhǔn)確問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03Python生成任意波形并存為txt的實(shí)現(xiàn)
本文主要介紹了Python生成任意波形并存為txt的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Python使用裝飾器進(jìn)行django開發(fā)實(shí)例代碼
這篇文章主要介紹了Python使用裝飾器進(jìn)行django開發(fā)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02使用django和vue進(jìn)行數(shù)據(jù)交互的方法步驟
這篇文章主要介紹了使用django和vue進(jìn)行數(shù)據(jù)交互的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11