{"id":19,"date":"2025-07-31T00:01:00","date_gmt":"2025-07-30T16:01:00","guid":{"rendered":"https:\/\/22z.top\/?p=19"},"modified":"2025-07-31T00:01:01","modified_gmt":"2025-07-30T16:01:01","slug":"flask%e6%9c%8d%e5%8a%a1%e7%ab%af%e9%a1%b9%e7%9b%ae%e6%90%ad%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/22z.top\/?p=19","title":{"rendered":"Flask\u670d\u52a1\u7aef\u9879\u76ee\u642d\u5efa"},"content":{"rendered":"\n<p>\u521b\u5efa\u865a\u62df\u73af\u5883<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">conda create -n yingming python=3.8<br>conda activate yingming<\/pre>\n\n\n\n<p>\u5b89\u88c5\u5f00\u53d1\u4e2d\u4f7f\u7528\u7684\u4f9d\u8d56\u6a21\u5757<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install flask<br>pip install flask-redis<br>pip install flask-mysqldb<br>pip install flask-sqlalchemy<br>pip install Flask-PyMongo<\/pre>\n\n\n\n<p>\u65b0\u5efa\u670d\u52a1\u7aef\u9879\u76ee\u6839\u76ee\u5f55yingmingapi\uff0c\u63a5\u7740\u5728pycharm\u4e2d\u6253\u5f00\u9879\u76ee\u76ee\u5f55yingmingapi\u7f16\u5199manage.py\u542f\u52a8\u7a0b\u5e8f\u6587\u4ef6<\/p>\n\n\n\n<p>\u5e38\u89c1\u7684\u7cfb\u7edf\u542f\u52a8\u7a0b\u5e8f\u6587\u4ef6\u540d\uff1amanage.py \/ run.py \/ main.py \/ home.py \/ index.py \/ common.py<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u542f\u52a8\u7a0b\u5e8f<\/h3>\n\n\n\n<p>manage.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>\u200b<br>app = Flask(__name__)<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;return 'index'<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<p>manage.py\u7ec8\u4e0d\u80fd\u5b58\u653e\u5927\u91cf\u7684\u5f00\u53d1\u4ee3\u7801, \u5728\u5f00\u53d1\u4e2d\u5e94\u8be5\u4f53\u73b0\u7684\u662f\u4e00\u79cd\u5206\u5de5\u7cbe\u795e,\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u628aflask\u4e2d\u5404\u79cd\u529f\u80fd\u4ee3\u7801\u8fdb\u884c\u5206\u7c7b\u5206\u6587\u4ef6\u5b58\u50a8.<\/p>\n\n\n\n<p>\u521b\u5efa\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u670d\u52a1\u7aef\u9879\u76ee\u6839\u76ee\u5f55\/<br>\u251c\u2500\u2500 application\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u9879\u76ee\u4e3b\u8981\u903b\u8f91\u4ee3\u7801\u4fdd\u5b58\u76ee\u5f55<br>| &nbsp; \u251c\u2500\u2500 settings\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u914d\u7f6e\u5b58\u50a8\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c dev.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u5f00\u53d1\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6\u3010\u672c\u5730\u5f00\u53d1\u8005\u4f7f\u7528\u3011<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c pro.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u751f\u4ea7\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6\u3010\u7ebf\u4e0a\u670d\u52a1\u5668\u4f7f\u7528\u3011<br>| &nbsp; | &nbsp; \u251c __init__.py &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u516c\u5171\u914d\u7f6e\u6587\u4ef6\uff0c\u76f8\u5f53\u4e8edjango\u7684global_settings.py<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u521d\u59cb\u5316\u5165\u53e3<br>\u251c\u2500\u2500 manage.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u7684\u7ec8\u7aef\u7ba1\u7406\u811a\u672c\u6587\u4ef6<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u5efa\u5168\u5c40\u521d\u59cb\u5316\u5de5\u5382\u51fd\u6570\u521b\u5efaapp\u5e94\u7528\u5bf9\u8c61<\/h3>\n\n\n\n<p>\u628a\u5f15\u5bfc\u6574\u4e2a\u9879\u76ee\u542f\u52a8\u7684\u5168\u5c40\u521d\u59cb\u5316\u4ee3\u7801,\u4fdd\u5b58\u5230<code>application\/__init__py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>\u200b<br>\u200b<br>def init_app() -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>manage.py,\u4e2d\u8c03\u7528\u521d\u59cb\u5316\u51fd\u6570,\u521b\u5efaapp\u5e94\u7528\u5b9e\u4f8b\u5bf9\u8c61,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from application import Flask, init_app<br>\u200b<br>\u200b<br>app: Flask = init_app()<br>\u200b<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;return 'welcome to yingmingApp'<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run(debug=True)<\/pre>\n\n\n\n<p>\u65b0\u589egit\u4e0a\u4f20\u7684\u5ffd\u7565\u6587\u4ef6\u5217\u8868\uff0c<code>.gitignore<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># Byte-compiled \/ optimized \/ DLL files<br>__pycache__\/<br>*.py[cod]<br>*$py.class<br>\u200b<br># C extensions<br>*.so<br>\u200b<br># Distribution \/ packaging<br>.Python<br>build\/<br>develop-eggs\/<br>dist\/<br>downloads\/<br>eggs\/<br>.eggs\/<br>lib\/<br>lib64\/<br>parts\/<br>sdist\/<br>var\/<br>wheels\/<br>share\/python-wheels\/<br>*.egg-info\/<br>.installed.cfg<br>*.egg<br>MANIFEST<br>\u200b<br># PyInstaller<br>#  Usually these files are written by a python script from a template<br>#  before PyInstaller builds the exe, so as to inject date\/other infos into it.<br>*.manifest<br>*.spec<br>\u200b<br># Installer logs<br>pip-log.txt<br>pip-delete-this-directory.txt<br>\u200b<br># Unit test \/ coverage reports<br>htmlcov\/<br>.tox\/<br>.nox\/<br>.coverage<br>.coverage.*<br>.cache<br>nosetests.xml<br>coverage.xml<br>*.cover<br>*.py,cover<br>.hypothesis\/<br>.pytest_cache\/<br>cover\/<br>\u200b<br># Translations<br>*.mo<br>*.pot<br>\u200b<br># Django stuff:<br>*.log<br>local_settings.py<br>db.sqlite3<br>db.sqlite3-journal<br>\u200b<br># Flask stuff:<br>instance\/<br>.webassets-cache<br>\u200b<br># Scrapy stuff:<br>.scrapy<br>\u200b<br># Sphinx documentation<br>docs\/_build\/<br>\u200b<br># PyBuilder<br>.pybuilder\/<br>target\/<br>\u200b<br># Jupyter Notebook<br>.ipynb_checkpoints<br>\u200b<br># IPython<br>profile_default\/<br>ipython_config.py<br>\u200b<br># pyenv<br># &nbsp; For a library or package, you might want to ignore these files since the code is<br># &nbsp; intended to run in multiple environments; otherwise, check them in:<br># .python-version<br>\u200b<br># pipenv<br># &nbsp; According to pypa\/pipenv#598, it is recommended to include Pipfile.lock in version control.<br># &nbsp; However, in case of collaboration, if having platform-specific dependencies or dependencies<br># &nbsp; having no cross-platform support, pipenv may install dependencies that don't work, or not<br># &nbsp; install all needed dependencies.<br>#Pipfile.lock<br>\u200b<br># poetry<br># &nbsp; Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.<br># &nbsp; This is especially recommended for binary packages to ensure reproducibility, and is more<br># &nbsp; commonly ignored for libraries.<br># &nbsp; https:\/\/python-poetry.org\/docs\/basic-usage\/#commit-your-poetrylock-file-to-version-control<br>#poetry.lock<br>\u200b<br># PEP 582; used by e.g. github.com\/David-OConnor\/pyflow<br>__pypackages__\/<br>\u200b<br># Celery stuff<br>celerybeat-schedule<br>celerybeat.pid<br>\u200b<br># SageMath parsed files<br>*.sage.py<br>\u200b<br># Environments<br>.env<br>.venv<br>env\/<br>venv\/<br>ENV\/<br>env.bak\/<br>venv.bak\/<br>\u200b<br># Spyder project settings<br>.spyderproject<br>.spyproject<br>\u200b<br># Rope project settings<br>.ropeproject<br>\u200b<br># mkdocs documentation<br>\/site<br>\u200b<br># mypy<br>.mypy_cache\/<br>.dmypy.json<br>dmypy.json<br>\u200b<br># Pyre type checker<br>.pyre\/<br>\u200b<br># pytype static type analyzer<br>.pytype\/<br>\u200b<br># Cython debug symbols<br>cython_debug\/<br>\u200b<br># PyCharm<br>#  JetBrains specific template is maintainted in a separate JetBrains.gitignore that can<br>#  be found at https:\/\/github.com\/github\/gitignore\/blob\/main\/Global\/JetBrains.gitignore<br>#  and can be added to the global gitignore or merged into this file.  For a more nuclear<br>#  option (not recommended) you can uncomment the following to ignore the entire idea folder.<br>.idea\/<br>\u200b<br>\u200b<br>nginx\/ssl_certs\/<br>!.gitkeep<br>\u200b<br>data<\/pre>\n\n\n\n<p>\u8bb0\u5f55\u4ee3\u7801\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git init<br>git config user.name \"mooluo\"<br>git config user.email \"649641514@qq.com\"<br>git remote add origin git@gitee.com:mooluo_admin\/yingmingapi.git<br>ssh-keygen -C \"649641514@qq.com\"<br>cat ~\/.ssh\/id_rsa.pub &nbsp;# \u628a\u516c\u94a5\u590d\u5236\u5230gitee \u300c\u4e2a\u4eba\u8bbe\u7f6e\u300d-&gt;\u300c\u5b89\u5168\u8bbe\u7f6e\u300d-&gt;\u300cSSH\u516c\u94a5\u300d-&gt;\u300c\u6dfb\u52a0\u516c\u94a5\u300d<br>rm -rf .idea<br>git add .<br>git commit -m \"fix: crate app instance\"<br>git push -u origin master<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u57fa\u4e8eshell\u811a\u672c\u542f\u52a8\u9879\u76ee<\/h3>\n\n\n\n<p>\u9879\u76ee\u6839\u76ee\u5f55\u4e0b<code>yingmingapi\/command.sh<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env bash<br>export FLASK_APP=\"\/home\/moluo\/Desktop\/yingmingapi\/manage.py\"<br>export FLASK_DEBUG=True<br>\u200b<br>if [ $1 ]; then<br> &nbsp;if [ $1 == \"run\" ]; then<br> &nbsp; &nbsp;flask run --host=0.0.0.0 --port=5000<br> &nbsp;else<br> &nbsp; &nbsp;flask $1<br> &nbsp;fi<br>fi<\/pre>\n\n\n\n<p>\u7ec8\u7aef\u4e0b\u8bbe\u7f6e\u6267\u884c\u6743\u9650<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">cd ~\/Desktop\/yingmingapi<br>chmod 755 command.sh<br>.\/command.sh run<\/pre>\n\n\n\n<p>\u7ec8\u7aef\u8fd0\u884c\u6548\u679c\uff1a<\/p>\n\n\n\n\n\n<h3 class=\"wp-block-heading\">\u52a0\u8f7d\u9879\u76ee\u914d\u7f6e<\/h3>\n\n\n\n<p>\u5728application\/utils\/config.py\u4e2d\u51c6\u5907\u52a0\u8f7d\u914d\u7f6e\u7684\u51fd\u6570\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>\u200b<br>\u200b<br>class Config(object):<br> &nbsp; &nbsp;\"\"\"\u9879\u76ee\u914d\u7f6e\u52a0\u8f7d\u7c7b\"\"\"<br> &nbsp; &nbsp;def __init__(self, app: Flask = None, config_path: str = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app, config_path)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask = None, path: str = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u9879\u76ee\u914d\u7f6e\u521d\u59cb\u5316\u51fd\u6570<br> &nbsp; &nbsp; &nbsp;  :param app: \u5f53\u524dflask\u5e94\u7528\u5b9e\u4f8b\u5bf9\u8c61[python\u4e2d\u7684\u5bf9\u8c61\u5c5e\u4e8e\u5f15\u7528\u7c7b\u578b\uff0c\u6240\u4ee5\u51fd\u6570\u5185\u90e8\u6539\u4e86app\u7684\u6570\u636e\uff0c\u5916\u754c\u7684app\u4e5f\u4f1a\u4fee\u6539]<br> &nbsp; &nbsp; &nbsp;  :param path: \u914d\u7f6e\u6587\u4ef6\u7684\u5bfc\u5305\u8def\u5f84 &nbsp; application.settings.dev<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u5148\u52a0\u8f7d\u9ed8\u8ba4\u914d\u7f6esettings.__init__\uff0c\u7136\u540e\u52a0\u8f7d\u5f53\u524d\u6307\u5b9a\u914d\u7f6econfig_path<br> &nbsp; &nbsp; &nbsp; &nbsp;init_path: str = \".\".join(path.split(\".\")[:-1])<br> &nbsp; &nbsp; &nbsp; &nbsp;app.config.from_object(init_path) &nbsp;# \u5148\u52a0\u8f7dsettings.__init__<br> &nbsp; &nbsp; &nbsp; &nbsp;app.config.from_object(path) &nbsp;# \u518d\u52a0\u8f7dsettings.dev\u6216\u8005settings.prod<\/pre>\n\n\n\n<p>\u7f16\u5199\u9879\u76ee\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6, <code>application\/settings\/__init__.py<\/code>\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u516c\u5171\u914d\u7f6e\"\"\"<br># \u8c03\u8bd5\u6a21\u5f0f<br>DEBUG: bool = False<br># \u8bed\u8a00<br>LANGUAGE: str = \"en\"<br># \u65f6\u533a<br>TZ: str = \"UTC\"<br>\u200b<\/pre>\n\n\n\n<p>\u5f53\u7136, \u9879\u76ee\u5f00\u53d1\u8fc7\u7a0b\u5b8c\u6210\u4ee5\u540e\u80af\u5b9a\u4f1a\u9879\u76ee\u4e0a\u7ebf,\u6240\u4ee5\u9488\u5bf9\u914d\u7f6e\u6587\u4ef6,\u6211\u4eec\u53ef\u4ee5\u51c6\u5907\u4e0d\u540c\u73af\u5883\u4e0b\u7684\u914d\u7f6e<\/p>\n\n\n\n<p><code>application\/settings\/dev.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u672c\u5730\u914d\u7f6e\"\"\"<br># \u8c03\u8bd5\u6a21\u5f0f<br>DEBUG: bool = True<br># \u8bed\u8a00<br>LANGUAGE: str = \"zh_hans\"<br># \u65f6\u533a<br>TZ: str = \"Asia\/Shanghai\"<br>\u200b<\/pre>\n\n\n\n<p><code>application\/settings\/pro.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u751f\u4ea7\u914d\u7f6e\"\"\"<br># \u8c03\u8bd5\u6a21\u5f0f<br>DEBUG: bool = False<br># \u8bed\u8a00<br>LANGUAGE: str = \"zh_hans\"<br># \u65f6\u533a<br>TZ: str = \"UTC\"<br>\u200b<\/pre>\n\n\n\n<p>\u5728\u9879\u76ee\u5165\u53e3\u6587\u4ef6<code>application\/__init__py<\/code>\u4e2d\u52a0\u8f7d\u914d\u7f6e,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>from application.utils.config import Config<br>\u200b<br>\u200b<br># \u5b9e\u4f8b\u5316\u914d\u7f6e\u52a0\u8f7d\u7c7b<br>config: Config = Config()<br>\u200b<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;config.init_app(app, config_path)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u5728\u521b\u5efaapp\u5bf9\u8c61\u7684\u9879\u76ee\u542f\u52a8\u6587\u4ef6manage.py\u4e2d,\u8bbe\u7f6e\u914d\u7f6e\u8def\u5f84<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from application import Flask, init_app<br>\u200b<br>app: Flask = init_app(\"application.settings.dev\")<br>\u200b<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;return 'welcome to yingmingApp'<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: add config function, init porject config\"<br>git push<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u5e93\u521d\u59cb\u5316<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">SQLAlchemy\u521d\u59cb\u5316<\/h4>\n\n\n\n<p>\u9ed8\u8ba4\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u4e2d\u589e\u52a0\u914d\u7f6e\u9009\u9879,<code>application\/settings\/__init__.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u516c\u5171\u914d\u7f6e\"\"\"<br>\u200b<br>\u200b<br>\"\"\"\u8c03\u8bd5\u6a21\u5f0f\"\"\"<br>DEBUG: bool = False<br>\u200b<br>\"\"\"\u672c\u5730\u5316\u56fd\u9645\u5316\"\"\"<br># \u8bed\u8a00<br>LANGUAGE: str = \"en\"<br>\u200b<br>\u200b<br>\"\"\"\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"\"<br>\u200b<br># \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e<br>SQLALCHEMY_TRACK_MODIFICATIONS: bool = False<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = False<\/pre>\n\n\n\n<p>\u5f00\u53d1\u914d\u7f6e<code>settings\/dev.py<\/code>\u4e2d,\u914d\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u672c\u5730\u914d\u7f6e\"\"\"<br># \u8c03\u8bd5\u6a21\u5f0f<br>DEBUG: bool = True<br># \u8bed\u8a00<br>LANGUAGE: str = \"zh_hans\"<br>\u200b<br>\"\"\"\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"mysql:\/\/yingminguser:yingming@127.0.0.1:3306\/yingming?charset=utf8mb4\"<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = True<\/pre>\n\n\n\n<p>\u5728mysql\u7ec8\u7aef\u4e0b, \u521b\u5efa\u5c5e\u4e8e\u5f53\u524d\u9879\u76ee\u7684\u6570\u636e\u5e93\u7ba1\u7406\u7528\u6237, \u547d\u4ee4\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">mysql -uroot -p123<br>create database yingming charset=utf8mb4;<br># \u9488\u5bf9\u5f53\u524d\u6570\u636e\u5e93\u914d\u7f6e\u8d26\u6237\u4fe1\u606f<br># mysql8.0\u4e4b\u540e<br>create user 'yingminguser'@'%' identified with mysql_native_password by 'yingming';<br>GRANT ALL ON yingming.* TO 'yingminguser'@'%';<br>\u200b<br># mysql8.0\u4e4b\u524d<br># create user yingminguser identified by 'yingming'; <br># grant all privileges on yingming.* to 'yingminguser'@'%';<br># flush privileges;<\/pre>\n\n\n\n<p>\u5728\u9879\u76ee\u521d\u59cb\u5316\u4e3b\u7a0b\u5e8f\u4e2d,\u5bf9\u6570\u636e\u5e93\u529f\u80fd\u8fdb\u884c\u521d\u59cb\u5316,<code>application\/__init__.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>\u200b<br>from application.utils.config import init_config<br>\u200b<br># SQLAlchemy\u521d\u59cb\u5316<br>db: SQLAlchemy = SQLAlchemy()<br>\u200b<br>def init_app(config_path: str):<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;init_config(app, config_path)<br> &nbsp; &nbsp;# print(app.config)<br>\u200b<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: connect mysql\"<br>git push<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Redis\u6570\u636e\u5e93\u521d\u59cb\u5316<\/h4>\n\n\n\n<p>\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\uff0c<code>application\/settings\/__init__.py<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u516c\u5171\u914d\u7f6e\"\"\"<br>\u200b<br>\u200b<br>\"\"\"\u8c03\u8bd5\u6a21\u5f0f\"\"\"<br>DEBUG: bool = False<br>\u200b<br>\"\"\"\u672c\u5730\u5316\u56fd\u9645\u5316\"\"\"<br># \u8bed\u8a00<br>LANGUAGE: str = \"en\"<br>\u200b<br>\u200b<br>\"\"\"mysql\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"\"<br>\u200b<br># \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e<br>SQLALCHEMY_TRACK_MODIFICATIONS: bool = False<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = False<br>\u200b<br>\"\"\"redis\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br>REDIS_URL = \"redis:\/\/@127.0.0.1:6379\/0\"<br>\u200b<\/pre>\n\n\n\n<p>\u5f00\u53d1\u914d\u7f6e\u6587\u4ef6\uff0c<code>application\/settings\/dev.py<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u672c\u5730\u914d\u7f6e\"\"\"<br>\u200b<br>\u200b<br>\"\"\"\u8c03\u8bd5\u6a21\u5f0f\"\"\"<br>DEBUG: bool = True<br>\u200b<br>\u200b<br>\"\"\"\u672c\u5730\u5316\u56fd\u9645\u5316\"\"\"<br># \u8bed\u8a00<br>LANGUAGE: str = \"zh_hans\"<br>\u200b<br>\u200b<br>\"\"\"\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"mysql:\/\/yingminguser:yingming@127.0.0.1:3306\/yingming?charset=utf8mb4\"<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = True<br>\u200b<br>\u200b<br>\"\"\"redis\u914d\u7f6e\"\"\"<br># \u9ed8\u8ba4\u7f13\u5b58\u6570\u636e<br>REDIS_URL: str = \"redis:\/\/:@127.0.0.1:6379\/0\"<br># \u9a8c\u8bc1\u76f8\u5173\u7f13\u5b58<br>CHECK_URL: str  = \"redis:\/\/:@127.0.0.1:6379\/1\"<\/pre>\n\n\n\n<p>\u5728\u9879\u76ee\u521d\u59cb\u5316\u4e3b\u7a0b\u5e8f\u4e2d, \u5bf9redis\u8fdb\u884c\u521d\u59cb\u5316,<code>applicaiton\/__init__.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u9879\u76ee\u521d\u59cb\u5316\u4e3b\u7a0b\u5e8f<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>\u200b<br>from application.utils.config import Config<br>\u200b<br>\u200b<br>\"\"\"\u52a0\u8f7d\u7ec4\u4ef6[\u5355\u4f8b\u6a21\u5f0f]\"\"\"<br>\u200b<br># \u521d\u59cb\u5316\u914d\u7f6e\u52a0\u8f7d\u7c7b<br>config: Config = Config()<br>\u200b<br># SQLAlchemy\u521d\u59cb\u5316<br>db: SQLAlchemy = SQLAlchemy()<br>\u200b<br># redis\u521d\u59cb\u5316<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br>\u200b<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;config.init_app(app, config_path)<br>\u200b<br> &nbsp; &nbsp;# \u52a0\u8f7dmysql\u6570\u636e\u5e93\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">mongoDB\u6570\u636e\u5e93\u521d\u59cb\u5316<\/h4>\n\n\n\n<p>\u5728\u63a5\u4e0b\u6765\u7684\u9879\u76ee\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u4f1a\u5148\u4f7f\u7528pymongo\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\uff0c\u540e\u9762\u6539\u6210MongoEngineORM\u6846\u67b6\u3002\u6240\u4ee5\u6682\u65f6\u6211\u4eec\u5148\u8fdb\u884cpymongo\u7684\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p>\u8fdb\u5165mongoDB\u4ea4\u4e92\u7ec8\u7aef\uff0c\u5206\u914d\u4e00\u4e2a\u8d26\u53f7\u7ed9\u5f53\u524d\u9879\u76ee<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5982\u679c\u6709mongodb\u7684\u8d26\u6237\u7ba1\u7406\u5458\uff0c\u5219\u76f4\u63a5\u8fdb\u5165mongoDB\u7ec8\u7aef\uff0c\u901a\u8fc7\u5982\u4e0b\u64cd\u4f5c\u521b\u5efayingming\u6570\u636e\u5e93\u7684\u7ba1\u7406\u5458<br>mongosh<br>use admin<br>db.auth(\"root\",\"123456\")<br>\u200b<br># \u7ed9yingming\u6570\u636e\u5e93\u521b\u5efa\u4e00\u4e2a\u6570\u636e\u5e93\u62e5\u6709\u8005\u8eab\u4efd\u7684\u7ba1\u7406\u5458\u8d26\u53f7<br>use yingming<br>db.createUser({<br> &nbsp;  user: \"yingming\",<br> &nbsp;  pwd: \"yingming\",<br> &nbsp;  roles: [<br> &nbsp; &nbsp; &nbsp;  { role: \"dbOwner\", db: \"yingming\"}<br> &nbsp;  ]<br>})<\/pre>\n\n\n\n<p><code>application\/settings\/__init__.py<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u516c\u5171\u914d\u7f6e\"\"\"<br>\u200b<br>\u200b<br>\"\"\"\u8c03\u8bd5\u6a21\u5f0f\"\"\"<br>DEBUG: bool = False<br>\u200b<br>\"\"\"\u672c\u5730\u5316\u56fd\u9645\u5316\"\"\"<br># \u8bed\u8a00<br>LANGUAGE: str = \"en\"<br>\u200b<br>\u200b<br>\"\"\"mysql\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"\"<br>\u200b<br># \u52a8\u6001\u8ffd\u8e2a\u4fee\u6539\u8bbe\u7f6e<br>SQLALCHEMY_TRACK_MODIFICATIONS: bool = False<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = False<br>\u200b<br>\"\"\"redis\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br>REDIS_URL: str = \"redis:\/\/@127.0.0.1:6379\/0\"<br>\u200b<br>\u200b<br>\"\"\"mongoDB\u914d\u7f6e\"\"\"<br># mongodb:\/\/[username:password@]host1[:port1][,...hostN[:portN]][\/[defaultauthdb][?options]]<br>MONGO_URI: str = \"mongodb:\/\/:@127.0.0.1:27017\/admin\"<br>\u200b<\/pre>\n\n\n\n<p>settings\/dev.py\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u672c\u5730\u914d\u7f6e\"\"\"<br>\u200b<br>\u200b<br>\"\"\"\u8c03\u8bd5\u6a21\u5f0f\"\"\"<br>DEBUG: bool = True<br>\u200b<br>\u200b<br>\"\"\"\u672c\u5730\u5316\u56fd\u9645\u5316\"\"\"<br># \u8bed\u8a00<br>LANGUAGE: str = \"zh_hans\"<br>\u200b<br>\u200b<br>\"\"\"\u6570\u636e\u5e93\u914d\u7f6e\"\"\"<br># \u6570\u636e\u5e93\u8fde\u63a5<br>SQLALCHEMY_DATABASE_URI: str = \"mysql:\/\/yingminguser:yingming@127.0.0.1:3306\/yingming?charset=utf8mb4\"<br>\u200b<br># \u67e5\u8be2\u65f6\u4f1a\u663e\u793a\u539f\u59cbSQL\u8bed\u53e5<br>SQLALCHEMY_ECHO: bool = True<br>\u200b<br>\u200b<br>\"\"\"redis\u914d\u7f6e\"\"\"<br># \u9ed8\u8ba4\u7f13\u5b58\u6570\u636e<br>REDIS_URL: str = \"redis:\/\/:@127.0.0.1:6379\/0\"<br># \u9a8c\u8bc1\u76f8\u5173\u7f13\u5b58<br>CHECK_URL: str  = \"redis:\/\/:@127.0.0.1:6379\/1\"<br>\u200b<br>\"\"\"mongoDB\u914d\u7f6e\"\"\"<br>MONGO_URI: str = \"mongodb:\/\/yingming:yingming@127.0.0.1:27017\/yingming\"<br>\u200b<\/pre>\n\n\n\n<p><code>application\/__init__.py<\/code>\uff0c\u5bf9mongoDB\u8fdb\u884c\u521d\u59cb\u5316<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>from flask_pymongo import PyMongo<br>\u200b<br>from application.utils.config import init_config<br>\u200b<br>\"\"\"\u52a0\u8f7d\u7ec4\u4ef6[\u5355\u4f8b\u6a21\u5f0f]\"\"\"<br># SQLAlchemy\u5b9e\u4f8b\u5316<br>db: SQLAlchemy = SQLAlchemy()<br>\u200b<br># redis\u5b9e\u4f8b\u5316<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br># mongoDB\u5b9e\u4f8b\u5316<br>mongo: PyMongo = PyMongo()<br>\u200b<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br>\u200b<br> &nbsp; &nbsp;# \u521d\u59cb\u5316\u9879\u76ee\u914d\u7f6e<br> &nbsp; &nbsp;init_config(app=app, config_path=config_path)<br>\u200b<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# pymongo\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;mongo.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: add redis configration and mongodb configration\"<br>git push<\/pre>\n\n\n\n<p>\u5b8c\u6210\u4e0a\u9762\u7684\u9879\u76ee\u6784\u5efa\u6b65\u9aa4\u4ee5\u540e\uff0c\u6b64\u65f6\u76ee\u5f55\u7ed3\u6784\u65b0\u589elogs\u76ee\u5f55\uff0c\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yingmingapi\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u9879\u76ee\u6839\u76ee\u5f55<br>\u251c\u2500\u2500 logs\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u65e5\u5fd7\u5b58\u50a8\u76ee\u5f55<br>| &nbsp; \u2514 yingming.log &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u9879\u76ee\u65e5\u5fd7<br>\u251c\u2500\u2500 application\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u9879\u76ee\u4e3b\u8981\u903b\u8f91\u4ee3\u7801\u4fdd\u5b58\u76ee\u5f55<br>| &nbsp; \u251c\u2500\u2500 settings\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u914d\u7f6e\u5b58\u50a8\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c dev.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5f00\u53d1\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c pro.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u751f\u4ea7\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6<br>| &nbsp; | &nbsp; \u2514 __init__.py &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u516c\u5171\u914d\u7f6e\u6587\u4ef6<br>| &nbsp; \u251c\u2500\u2500 utils\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5de5\u5177\u51fd\u6570\u5e93\/\u7c7b\u5e93<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u2514 config.py &nbsp; &nbsp; &nbsp; &nbsp; # \u914d\u7f6e\u76f8\u5173\u7684\u8f85\u52a9\u7c7b\u6216\u8005\u8f85\u52a9\u51fd\u6570<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 __init__.py &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u521d\u59cb\u5316\u6587\u4ef6[\u5165\u53e3\u7a0b\u5e8f\uff0cAPP\u5de5\u5382\u51fd\u6570]<br>\u251c\u2500\u2500 manage.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u7684\u7ec8\u7aef\u7ba1\u7406\u811a\u672c\u6587\u4ef6<br>\u2514\u2500\u2500 command.sh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7ec8\u7aefshell\u547d\u4ee4\u811a\u672c\uff0c\u7528\u4e8e\u5b8c\u6210\u7ec8\u7aef\u64cd\u4f5c\u548c\u542f\u52a8\u9879\u76ee<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u65e5\u5fd7\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>flask\u4e2d\u672c\u8eab\u5185\u7f6e\u4e86\u57fa\u4e8eloging\u6a21\u5757\u5c01\u88c5\u7684\u65e5\u5fd7\u529f\u80fd\u7684,\u6211\u4eec\u5728\u4f7f\u7528\u7684\u65f6\u5019, \u4e00\u822c\u65e5\u5fd7\u5982\u679c\u4e0d\u662f\u6838\u5fc3\u91cd\u70b9,\u5219\u901a\u8fc7\u7531python\u5185\u7f6e\u7684logging\u6a21\u5757\u8fdb\u884c\u914d\u7f6e\u96c6\u6210\u4f7f\u7528\u5373\u53ef, \u5982\u679c\u9879\u76ee\u4e2d\u65e5\u5fd7\u53d1\u6325\u4f5c\u7528\u6bd4\u8f83\u91cd\u8981, \u5219\u4e00\u822c\u5b89\u88c5\u90e8\u7f72ELK\u65e5\u5fd7\u5206\u6790\u7cfb\u7edf.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u65e5\u5fd7\u7684\u7b49\u7ea7<\/h4>\n\n\n\n<pre class=\"wp-block-preformatted\">\u4ece\u9ad8\u5230\u4f4e\uff0c\u4f9d\u6b21\uff1a<br>    FATAL\/CRITICAL = \u81f4\u547d\u7684\uff0c\u5371\u9669\u7684<br>    ERROR = \u9519\u8bef<br>    WARNING = \u8b66\u544a<br>    INFO = \u4fe1\u606f<br>    DEBUG = \u8c03\u8bd5<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">flask\u65e5\u5fd7\u529f\u80fd\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n\n\n\n<p>manage.py<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from application import Flask, init_app<br>\u200b<br>app: Flask = init_app(\"application.settings.dev\")<br>\u200b<br>\u200b<br>@app.route('\/')<br>def index():<br> &nbsp; &nbsp;app.logger.debug(\"hello, debug\")<br> &nbsp; &nbsp;app.logger.info(\"hello, info\")<br> &nbsp; &nbsp;app.logger.warning(\"hello, warning\")<br> &nbsp; &nbsp;app.logger.error(\"hello, error\")<br> &nbsp; &nbsp;app.logger.critical(\"hello, critical\")<br> &nbsp; &nbsp;return 'welcome to yingmingApp'<br>\u200b<br>\u200b<br>if __name__ == '__main__':<br> &nbsp; &nbsp;app.run()<\/pre>\n\n\n\n<p>\u5728\u9879\u76ee\u8fd0\u884c\u65f6\uff0c\u6846\u67b6\u672c\u8eab\u4f1a\u4e0d\u65ad\u8bb0\u5f55\u4ee3\u7801\u7a0b\u5e8f\u8fd0\u884c\u7684\u65e5\u5fd7\u9519\u8bef\uff0c\u4f46\u662f\u6211\u4eec\u5c06\u6765\u7f16\u5199\u7684\u4e1a\u52a1\u903b\u8f91\u4e5f\u53ef\u80fd\u5b58\u5728\u629b\u51fa\u5f02\u5e38\u7684\u60c5\u51b5\uff0c\u8fd9\u4e9b\u5f02\u5e38\u548c\u6846\u67b6\u4ee3\u7801\u672c\u8eab\u65e0\u5173\uff0c\u5982\u679c\u90fd\u6df7\u5728\u4e00\u8d77\u7684\u8bdd\uff0c\u5bf9\u4e8e\u627e\u51fa\u9519\u8bef\uff0c\u662f\u6ca1\u6709\u5e2e\u52a9\u7684\u3002\u6240\u4ee5\uff0c\u6211\u4eec\u5728\u8fd9\u91cc\u65b0\u5efa\u4e00\u4e2a\u65e5\u5fd7\u8bb0\u5f55\u5668\uff0c\u4e13\u95e8\u53ea\u662f\u8bb0\u5f55\u6211\u4eec\u4e1a\u52a1\u903b\u8f91\u7684\u76f8\u5173\u65e5\u5fd7\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6784\u5efa\u65e5\u5fd7\u6a21\u5757<\/h4>\n\n\n\n<p>\u628a\u65e5\u5fd7\u521d\u59cb\u5316\u76f8\u5173\u7684\u4ee3\u7801\u5c01\u88c5\u6210\u4e00\u4e2a\u65e5\u5fd7\u914d\u7f6e\u7c7bLogger\uff0c<code>application\/utils\/logger.py<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import logging<br>from logging.handlers import RotatingFileHandler &nbsp; &nbsp; &nbsp; # \u6309\u6587\u4ef6\u5927\u5c0f\u5206\u5272\u65e5\u5fd7\u6587\u4ef6<br>from logging.handlers import TimedRotatingFileHandler &nbsp; &nbsp;# \u6309\u65f6\u95f4\u7247\u5206\u5272\u65e5\u5fd7\u6587\u4ef6<br>from flask import Flask<br>\u200b<br>class Logger(object):<br> &nbsp; &nbsp;\"\"\"\u65e5\u5fd7\u914d\u7f6e\u7c7b\"\"\"<br> &nbsp; &nbsp;def __init__(self, app: Flask = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u65e5\u5fd7\u5b9e\u4f8b\u5316<br> &nbsp; &nbsp; &nbsp;  :param app: \u5f53\u524dflask\u5e94\u7528\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask = None)-&gt; None:<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u8bfb\u53d6\u9879\u76ee\u7684\u65e5\u5fd7\u914d\u7f6e\u9879<br> &nbsp; &nbsp; &nbsp;  :param app: \u5f53\u524dflask\u5e94\u7528\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp;  :return: None<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;self.app = app<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_file = self.app.BASE_DIR \/ self.app.config.get(\"LOG_FILE\", 'logs\/app.log')<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_level = self.app.config.get(\"LOG_LEVEL\", 'INFO')<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_backpu_count = self.app.config.get(\"LOG_BACKPU_COUNT\", 31)<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_format = self.app.config.get(\"LOG_FORMAT\", '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s')<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_rotating_time = self.app.config.get(\"LOG_ROTATING_TIME\", \"midnight\")<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_charseter = self.app.config.get(\"LOG_charseter\", 'UTF-8')<br> &nbsp; &nbsp; &nbsp; &nbsp;self.log_file_size = self.app.config.get(\"LOG_FILE_SIZE\", 300*1024*1024)<br> &nbsp; &nbsp; &nbsp; &nbsp;self.setup()<br>\u200b<br> &nbsp; &nbsp;def setup(self)-&gt; None:<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u628a\u65e5\u5fd7\u529f\u80fd\u5b89\u88c5\u5230flask\u9879\u76ee\u4e2d<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# from logging.handlers import TimedRotatingFileHandler \u6309\u65f6\u95f4\u7247\u5206\u5272\u65e5\u5fd7<br> &nbsp; &nbsp; &nbsp; &nbsp;handler: TimedRotatingFileHandler = TimedRotatingFileHandler(<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filename=self.log_file, &nbsp;# \u65e5\u5fd7\u5b58\u50a8\u7684\u6587\u4ef6\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;when=self.log_rotating_time, &nbsp;# \u6bcf\u5929\u5907\u4efd\u65e5\u5fd7\u7684\u65f6\u95f4\uff0c\u5348\u591c<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;backupCount=self.log_backpu_count, &nbsp;# \u5907\u4efd\u6570\u91cf<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;encoding=self.log_charseter # \u65e5\u5fd7\u6587\u4ef6\u7f16\u7801<br> &nbsp; &nbsp; &nbsp;  )<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# from logging.handlers import RotatingFileHandler &nbsp; &nbsp;  \u6309\u6587\u4ef6\u5927\u5c0f\u5206\u5272\u65e5\u5fd7<br> &nbsp; &nbsp; &nbsp; &nbsp;# handler: RotatingFileHandler = RotatingFileHandler(<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; filename=self.log_file,<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; maxBytes=self.log_file_size,<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; backupCount=self.log_backpu_count,<br> &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; encoding=self.log_charseter # \u65e5\u5fd7\u6587\u4ef6\u7f16\u7801<br> &nbsp; &nbsp; &nbsp; &nbsp;# )<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u8bbe\u7f6e\u65e5\u5fd7\u4fe1\u606f\u7684\u7b49\u7ea7<br> &nbsp; &nbsp; &nbsp; &nbsp;handler.setLevel(self.log_level)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u65e5\u5fd7\u4fe1\u606f\u7684\u683c\u5f0f<br> &nbsp; &nbsp; &nbsp; &nbsp;logging_format: logging.Formatter = logging.Formatter(self.log_format)<br> &nbsp; &nbsp; &nbsp; &nbsp;handler.setFormatter(logging_format)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;self.app.logger.addHandler(handler)<br>\u200b<\/pre>\n\n\n\n<p><code>application\/settings\/__init__.py<\/code>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u65e5\u5fd7\u914d\u7f6e\"\"\"<br>LOG_FILE: str = \"logs\/app.log\"<br>LOG_LEVEL: str = \"INFO\"<br>LOG_BACKPU_COUNT: int = 31<br>LOG_FORMAT: str = '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'<\/pre>\n\n\n\n<p><code>application\/settings\/dev.py<\/code>\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u65e5\u5fd7\u914d\u7f6e\"\"\"<br>LOG_FILE: str = \"logs\/yingming.log\"<br>LOG_LEVEL: str = \"DEBUG\"<br>LOG_BACKPU_COUNT: int = 31<br>LOG_FORMAT: str = '%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)s - %(message)s'<\/pre>\n\n\n\n<p>\u5728 <code>application\/__init__.py<\/code>\u6587\u4ef6\u4e2d\u7684<code>init_app<\/code> \u65b9\u6cd5\u4e2d\u8c03\u7528\u65e5\u5fd7\u7c7b\u8fdb\u884c\u65e5\u5fd7\u529f\u80fd\u7684\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from pathlib import Path<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>from flask_pymongo import PyMongo<br>\u200b<br>from application.utils.config import Config<br>from application.utils.logger import Logger<br>\u200b<br># \u5b9e\u4f8b\u5316\u914d\u7f6e\u52a0\u8f7d\u7c7b<br>config: Config = Config()<br># \u5b9e\u4f8b\u5316SQLAlchemy<br>db: SQLAlchemy = SQLAlchemy()<br># \u5b9e\u4f8b\u5316redis<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br># mongoDB\u5b9e\u4f8b\u5316<br>mongo: PyMongo = PyMongo()<br># \u5b9e\u4f8b\u5316\u65e5\u5fd7\u914d\u7f6e\u7c7b<br>logger: Logger = Logger()<br>\u200b<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;# \u5168\u5c40\u8def\u5f84\u5e38\u91cf\uff0c\u6307\u5411\u9879\u76ee\u6839\u76ee\u5f55<br> &nbsp; &nbsp;app.BASE_DIR: Path = Path(__file__).resolve().parent.parent<br>\u200b<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;config.init_app(app, config_path)<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br> &nbsp; &nbsp;# # pymongo\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;mongo.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u65e5\u5fd7\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;logger.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u5b8c\u6210\u65e5\u5fd7\u521d\u59cb\u5316\u914d\u7f6e\u4ee5\u540e\u7684\u9879\u76ee\u76ee\u5f55\u7ed3\u6784<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yingmingapi\/<br>\u251c\u2500\u2500 application\/<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 settings\/<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 dev.py<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 pro.py<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 utils\/<br>\u2502&nbsp;&nbsp; &nbsp; &nbsp; \u251c\u2500\u2500 config.py<br>\u2502&nbsp;&nbsp; &nbsp; &nbsp; \u251c\u2500\u2500 __init__.py<br>\u2502&nbsp;&nbsp; &nbsp; &nbsp; \u2514\u2500\u2500 logger.py &nbsp; &nbsp; &nbsp;# \u65e5\u5fd7\u76f8\u5173\u6a21\u5757\u4ee3\u7801<br>\u251c\u2500\u2500 logs\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u65e5\u5fd7\u6587\u4ef6\u5b58\u50a8\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 yingming.log<br>\u251c\u2500\u2500 .gitignore<br>\u251c\u2500\u2500 command.sh<br>\u2514\u2500\u2500 manage.py<\/pre>\n\n\n\n<p>\u7ecf\u8fc7\u4e0a\u9762\u7684\u6539\u9020\uff0c\u6211\u4eec\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u5f00\u59cb\u521b\u5efa\u84dd\u56fe\u4e86\u3002<\/p>\n\n\n\n<p>\u63d0\u4ea4\u4ee3\u7801<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: add log\"<br>git push<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u84dd\u56fe\u521d\u59cb\u5316<\/h3>\n\n\n\n<p>\u5728application\u4e0b\u521b\u5efaapps\u76ee\u5f55\uff0capps\u4ee5\u540e\u4e13\u95e8\u7528\u4e8e\u4fdd\u5b58\u9879\u76ee\u7684\u6bcf\u4e00\u4e2a\u84dd\u56fe\u5e94\u7528\uff0c\u5e76\u5728apps\u521b\u5efahome\u84dd\u56fe\u76ee\u5f55,\u5e76\u5728<code>home\/__init__.py<\/code>\u6587\u4ef6\u4e2d\u521b\u5efa\u84dd\u56fe\u5bf9\u8c61\uff0c\u8fd8\u6709\u84dd\u56fe\u76ee\u5f55\u4e0b\u82e5\u5e72\u6587\u4ef6\uff0c\u4f8b\u5982\u89c6\u56fe\u3001\u6a21\u578b\u3001\u8def\u7531\u3001\u5e8f\u5217\u5316\u5668\u7b49\u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.<br>\u2514\u2500\u2500 application<br> &nbsp;&nbsp; \u251c\u2500\u2500 __init__.py<br> &nbsp;&nbsp; \u2514\u2500\u2500 apps\/<br> &nbsp;&nbsp; &nbsp; &nbsp; \u2514\u2500 home\/<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 __init__.py<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 admin.py &nbsp; &nbsp; &nbsp;  # admin\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 serializers.py  # \u5e8f\u5217\u5316\u6587\u4ef6  marshmallow<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 models.py &nbsp; &nbsp; &nbsp; # mysql\u6a21\u578b<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 documents.py &nbsp; &nbsp; # mongoDB\u6a21\u578b<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 tasks.py &nbsp; &nbsp; &nbsp;  # celery\u7684\u5f02\u6b65\u4efb\u52a1<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 ws.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # websocket \u7684\u89c6\u56fe\u6587\u4ef6[websocket\u63a5\u53e3\u89c6\u56fe]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u251c\u2500 api.py &nbsp; &nbsp; &nbsp; &nbsp;  # api \u7684\u89c6\u56fe\u6587\u4ef6[api\u63a5\u53e3\u89c6\u56fe]<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 views.py &nbsp; &nbsp; &nbsp;  # http web\u89c6\u56fe\u63a5\u53e3 &nbsp; [\u666e\u901a\u89c6\u56fe]<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u251c\u2500 urls.py &nbsp; &nbsp; &nbsp; &nbsp; # \u5b50\u8def\u7531\u6587\u4ef6<br> &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;  \u2514\u2500 tests.py &nbsp; &nbsp; &nbsp;  # \u5355\u5143\u6d4b\u8bd5\u7684\u6d4b\u8bd5\u7528\u4f8b<\/pre>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u7ec8\u7aef\u547d\u4ee4, \u521b\u5efa\u4e00\u4e2a\u81ea\u52a8\u751f\u6210\u84dd\u56fe\u76ee\u5f55\u7684\u547d\u4ee4.<code>application\/utils\/commands.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import click, os<br>from typing import Optional<br>from flask import Flask<br>\u200b<br>class Command(object):<br> &nbsp; &nbsp;\"\"\"Flask\u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b\"\"\"<br> &nbsp; &nbsp;def __init__(self, app: Optional[Flask] = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask):<br> &nbsp; &nbsp; &nbsp; &nbsp;self.app: Flask = app<br> &nbsp; &nbsp; &nbsp; &nbsp;self.setup()<br>\u200b<br> &nbsp; &nbsp;def setup(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u521d\u59cb\u5316\u7ec8\u7aef\u547d\u4ee4\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;self.blueprint() &nbsp;# \u81ea\u52a8\u521b\u5efa\u84dd\u56fe\u76ee\u5f55\u548c\u6587\u4ef6<br>\u200b<br> &nbsp; &nbsp;def blueprint(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"\u84dd\u56fe\u76ee\u5f55\u751f\u6210\u547d\u4ee4\"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;@self.app.cli.command(\"blue\") &nbsp;# \u6307\u5b9a\u7ec8\u7aef\u547d\u4ee4\u7684\u8c03\u7528\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp;@click.option(\"--name\", default=\"home\", help=\"\u84dd\u56fe\u76ee\u5f55\u540d\u79f0\", type=str)<br> &nbsp; &nbsp; &nbsp; &nbsp;def command(name: str):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u751f\u6210\u84dd\u56fe\u540d\u79f0\u5bf9\u8c61\u7684\u76ee\u5f55<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;os.mkdir(name)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/__init__.py\" % name, \"w\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/admin.py\" % name, \"w\") &nbsp;# adnmin\u540e\u53f0\u7ad9\u70b9\u914d\u7f6e\u6587\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/ws.py\" % name, \"w\") &nbsp;# websocket\u7684\u89c6\u56fe\u6587\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/api.py\" % name, \"w\") &nbsp;# api\u63a5\u53e3\u7684\u89c6\u56fe\u6587\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/views.py\" % name, \"w\") &nbsp;# \u666e\u901a\u89c6\u56fe\u6587\u4ef6<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/models.py\" % name, \"w\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/documents.py\" % name, \"w\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/urls.py\" % name, \"w\") &nbsp;# \u89c6\u56fe\u8def\u7531<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/test.py\" % name, \"w\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/tasks.py\" % name, \"w\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;open(\"%s\/serializers.py\" % name, \"w\") &nbsp;# \u5e8f\u5217\u5316\u5668\u6587\u4ef6<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;print(\"BluePrint[%s] created done....\" % name)<br>\u200b<\/pre>\n\n\n\n<p>\u4e0a\u9762\u7684\u547d\u4ee4\u5c31\u53ef\u4ee5\u5e2e\u6211\u4eec\u5b8c\u6210\u9879\u76ee\u4e2d\u751f\u6210\u84dd\u56fe\u7684\u529f\u80fd,\u5c31\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u76f4\u63a5\u628a\u547d\u4ee4\u6ce8\u518c\u5230manage\u5bf9\u8c61\u4e2d\u5c31\u53ef\u4ee5\u4f7f\u7528\u4e86.<\/p>\n\n\n\n<p>\u5728\u9879\u76ee\u5165\u53e3\u6587\u4ef6<code>application\/__init__.py<\/code>\u4e2d, \u521d\u59cb\u5316\u7ec8\u7aef\u547d\u4ee4\u5373\u53ef\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"># \u5148\u5199 import.. \u540e\u5199 from .. import ...<br># \u5148\u89e3\u6790\u5668\u5185\u7f6e\uff0c\u540e\u6846\u67b6\u5b98\u65b9\uff0c\u7136\u540e\u7b2c\u4e09\u65b9\u6a21\u5757\uff0c\u63a5\u7740\u662f\u81ea\u5df1\u5c01\u88c5\u7684\u672c\u5730\u6a21\u5757\u3002<br>\u200b<br>from pathlib import Path<br>\u200b<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>from flask_pymongo import PyMongo<br>\u200b<br>from application.utils.config import init_config<br>from application.utils.logger import Log<br>from application.utils.commands import Command<br>\u200b<br>\"\"\"\u52a0\u8f7d\u7ec4\u4ef6[\u5355\u4f8b\u6a21\u5f0f]\"\"\"<br># SQLAlchemy\u5b9e\u4f8b\u5316<br>db: SQLAlchemy = SQLAlchemy()<br>\u200b<br># redis\u5b9e\u4f8b\u5316<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br># mongoDB\u5b9e\u4f8b\u5316<br>mongo: PyMongo = PyMongo()<br>\u200b<br># \u65e5\u5fd7\u5b9e\u4f8b\u5316<br>logger:Log = Log()<br>\u200b<br># \u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b\u5b9e\u4f8b\u5316<br>command: Command = Command()<br>\u200b<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br>\u200b<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br>\u200b<br> &nbsp; &nbsp;# \u9879\u76ee\u6839\u76ee\u5f55<br> &nbsp; &nbsp;app.BASE_DIR = Path(__file__).resolve().parent.parent<br>\u200b<br> &nbsp; &nbsp;# \u521d\u59cb\u5316\u9879\u76ee\u914d\u7f6e<br> &nbsp; &nbsp;init_config(app=app, config_path=config_path)<br>\u200b<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# pymongo\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;mongo.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u65e5\u5fd7\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;logger.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;command.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u5c31\u53ef\u4ee5\u5728\u7ec8\u7aef\u4e0b,\u901a\u8fc7\u547d\u4ee4\u751f\u6210\u84dd\u56fe\u76ee\u5f55\u4e86\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u751f\u6210\u84dd\u56fe\u7684shell\u547d\u4ee4\uff0c<code>command.sh<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#!\/usr\/bin\/env bash<br>export FLASK_APP=\"\/home\/moluo\/Desktop\/yingmingapi\/manage.py\"<br>export FLASK_DEBUG=True<br>\u200b<br>if [ $1 ]; then<br> &nbsp;if [ $1 == \"run\" ]; then<br> &nbsp;  flask run --host=0.0.0.0 --port=5000<br> &nbsp;elif [ $1 == \"blue\" ]; then<br> &nbsp; &nbsp;cd application\/apps<br> &nbsp;  flask $1 --name=$2<br> &nbsp;else<br> &nbsp;  flask $1<br> &nbsp;fi<br>fi<\/pre>\n\n\n\n<p>\u7ec8\u7aef\u4e0b\u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">.\/command.sh blue home<\/pre>\n\n\n\n<p>\u6548\u679c:<\/p>\n\n\n\n\n\n<p>\u6709\u4e86\u84dd\u56fe\u4ee5\u540e,\u63a5\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u628a\u89c6\u56fe\u4ee3\u7801,\u6a21\u578b\u4ee3\u7801,\u8def\u7531\u4ee3\u7801\u7b49\u5b58\u50a8\u5230\u84dd\u56fe\u76ee\u5f55\u4e0b\u4e86,\u4f46\u662f\u6211\u4eec\u9700\u8981\u628a\u84dd\u56fe\u6ce8\u518c\u5230app\u5e94\u7528\u5bf9\u8c61\u4e0b\uff0c\u60f3\u60f3\u4ee5\u540e\uff0c\u662f\u4e0d\u662f\u4f1a\u51fa\u73b0\u5f88\u591a\u7684\u84dd\u56fe\uff1f<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u80fd\u4e0d\u80fd\u50cfdjango\u9879\u76ee\u4e2d\u7684\u914d\u7f6e\u9879INSTALLED_APPS\u91cc\u9762\u586b\u5199\u5b8c\u84dd\u56fe\u8def\u5f84\u5c31\u53ef\u4ee5\u81ea\u52a8\u6ce8\u518c\u84dd\u56fe\uff1f<\/p>\n\n\n\n<p>\u53ef\u4ee5\u7684\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5c01\u88c5\u6ce8\u518c\u84dd\u56fe\u7684\u529f\u80fd\u5230\u4e00\u4e2a\u51fd\u6570\u4e2d\uff0c\u8ba9\u9879\u76ee\u81ea\u52a8\u8bc6\u522b\u5e76\u6ce8\u518c\u84dd\u56fe\uff0c\u5e76\u81ea\u52a8\u52a0\u8f7d\u84dd\u56fe\u4e0b\u7684\u6240\u6709\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u58f0\u660e\u4e00\u4e2a\u84dd\u56fe\u6ce8\u518c\u5217\u8868\uff08\u4f8b\u5982\uff1aINSTALL_BLUEPRINTS\uff09, \u5728\u84dd\u56fe\u81ea\u52a8\u6ce8\u518c\u7684\u51fd\u6570\u4e2d\u53ea\u6ce8\u518c\u5217\u8868\u4e2d\u586b\u5199\u7684\u84dd\u56fe\u3002<\/p>\n\n\n\n<p><code>application\/settings\/__init__.py<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import List<br>\u200b<br>\"\"\"\u84dd\u56fe\u5217\u8868\"\"\"<br>INSTALL_BLUEPRINTS: List = [<br>\u200b<br>]<\/pre>\n\n\n\n<p><code>application\/settings\/dev.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import List<br>\u200b<br>\u200b<br>\"\"\"\u84dd\u56fe\u5217\u8868\"\"\"<br>INSTALL_BLUEPRINTS: List = [<br> &nbsp; &nbsp;\"application.apps.home\",<br>]<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u52a8\u6ce8\u518c\u84dd\u56fe<\/h4>\n\n\n\n<p>\u5728<code>application\/utils\/blueprint.py<\/code>\u6a21\u5757\u4e2d\u58f0\u660e\u4e00\u4e2aauto_resgister_blueprint\u51fd\u6570,\u51fd\u6570\u4e2d\u9488\u5bf9\u6ce8\u518c\u5230\u9879\u76ee\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684INSTALL_BLUEPRINTS\u84dd\u56fe\u5217\u8868\u9009\u9879\u7684\u84dd\u56fe\u5185\u5bb9\u5b9e\u73b0\u81ea\u52a8\u6ce8\u518c\u5230app\u5e94\u7528\u5bf9\u8c61\u91cc\u9762\u3002<\/p>\n\n\n\n<p><code>application\/utils\/blueprint.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import List, Optional<br>from flask import Flask, Blueprint<br>\u200b<br>\u200b<br>class AutoBluePrint(object):<br> &nbsp; &nbsp;def __init__(self, app: Optional[Flask] = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u81ea\u52a8\u6ce8\u518c\u84dd\u56fe<br> &nbsp; &nbsp; &nbsp;  :param app:<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u9700\u8981\u6ce8\u518c\u5230\u9879\u76ee\u4e2d\u7684\u84dd\u56fe\u8def\u5f84\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp;blueprint_path_list: List = app.config.get(\"INSTALL_BLUEPRINTS\", [])<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u904d\u5386\u84dd\u56fe\u8def\u5f84\u5217\u8868\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u84dd\u56fe\u8fdb\u884c\u521d\u59cb\u5316<br> &nbsp; &nbsp; &nbsp; &nbsp;for blueprint_path in blueprint_path_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u6bb5\u7684\u5305\u540d\u4f5c\u4e3a\u84dd\u56fe\u7684\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_name: str = blueprint_path.split(\".\")[-1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7ed9\u5f53\u524d\u84dd\u56fe\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a\u84dd\u56fe\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint: Blueprint = Blueprint(blueprint_name, blueprint_path)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u628a\u84dd\u56fe\u5bf9\u8c61\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# todo url_prefix \u662f\u5730\u5740\u524d\u7f00\uff0c\u5c06\u6765\u6211\u4eec\u5c06\u6765\u5b9e\u73b0\u4e00\u4e2a\u603b\u8def\u7531\u6765\u58f0\u660e\u5b83<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.register_blueprint(blueprint, url_prefix=\"\")<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;print(app.blueprints)<br>\u200b<\/pre>\n\n\n\n<p>\u9879\u76ee\u5165\u53e3\u6587\u4ef6<code>application\/__init__.py<\/code>\u4e2d, \u5b9e\u4f8b\u5316AutoBluePrint\u7c7b, \u81ea\u52a8\u751f\u6210\u5e76\u6ce8\u518c\u84dd\u56fe\u5bf9\u8c61\uff0c<code>application\/__init__.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from pathlib import Path<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>from flask_pymongo import PyMongo<br>\u200b<br>from application.utils.config import Config<br>from application.utils.logger import Logger<br>from application.utils.commands import Command<br>from application.utils.blueprint import AutoBluePrint<br>\u200b<br>\u200b<br># \u5b9e\u4f8b\u5316\u914d\u7f6e\u52a0\u8f7d\u7c7b<br>config: Config = Config()<br># \u5b9e\u4f8b\u5316SQLAlchemy<br>db: SQLAlchemy = SQLAlchemy()<br># \u5b9e\u4f8b\u5316redis<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br># mongoDB\u5b9e\u4f8b\u5316<br>mongo: PyMongo = PyMongo()<br># \u5b9e\u4f8b\u5316\u65e5\u5fd7\u914d\u7f6e\u7c7b<br>logger: Logger = Logger()<br>\u200b<br># \u5b9e\u4f8b\u5316\u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b<br>command: Command = Command()<br>\u200b<br># \u5b9e\u4f8b\u5316\u81ea\u52a8\u5316\u84dd\u56fe\u7c7b<br>blueprint: AutoBluePrint = AutoBluePrint()<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;# \u5168\u5c40\u8def\u5f84\u5e38\u91cf\uff0c\u6307\u5411\u9879\u76ee\u6839\u76ee\u5f55<br> &nbsp; &nbsp;app.BASE_DIR: Path = Path(__file__).resolve().parent.parent<br>\u200b<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;config.init_app(app, config_path)<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br> &nbsp; &nbsp;# # pymongo\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;mongo.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u65e5\u5fd7\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;logger.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;command.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u81ea\u52a8\u5316\u84dd\u56fe\u7c7b\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;blueprint.init_app(app)<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u6ce8\u518c\u4e86\u84dd\u56fe\u5bf9\u8c61\u4ee5\u540e,\u84dd\u56fe\u4e0b\u9762\u7684\u89c6\u56fe\u65b9\u6cd5\u548c\u89c6\u56fe\u5bf9\u5e94\u7684\u8def\u7531\u5173\u7cfb\u4e5f\u8981\u8fdb\u884c\u6ce8\u518c!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6ce8\u518c\u84dd\u56fe\u7684\u8def\u7531\u548c\u89c6\u56fe<\/h4>\n\n\n\n<p>\u5728\u84dd\u56fehome\u4e0b\u9762\u7684urls.py\u6587\u4ef6\u4e2d,\u901a\u8fc7path\u65b9\u6cd5\u628aurl\u5730\u5740\u548c\u89c6\u56fe\u65b9\u6cd5\u3001http\u8bf7\u6c42\u3001\u5904\u7406\u6210\u5b57\u5178\uff0c\u7136\u540e\u628a\u5b57\u5178\u4f5c\u4e3a\u6210\u5458\u6dfb\u52a0\u5230\u8def\u7531\u5217\u8868\u7684\u56fa\u5b9a\u53d8\u91cfurlpatterns\u5217\u8868\u4e2d.<\/p>\n\n\n\n<p><code>application\/utils\/blueprint.py<\/code>,\u58f0\u660epath\u51fd\u6570,\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from types import ModuleType<br>from typing import List, Optional, Callable<br>from flask import Flask, Blueprint<br>from importlib import import_module<br>\u200b<br>\u200b<br>class AutoBluePrint(object):<br> &nbsp; &nbsp;def __init__(self, app: Optional[Flask] = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u81ea\u52a8\u6ce8\u518c\u84dd\u56fe<br> &nbsp; &nbsp; &nbsp;  :param app:<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u9700\u8981\u6ce8\u518c\u5230\u9879\u76ee\u4e2d\u7684\u84dd\u56fe\u8def\u5f84\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp;blueprint_path_list: List = app.config.get(\"INSTALL_BLUEPRINTS\", [])<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u904d\u5386\u84dd\u56fe\u8def\u5f84\u5217\u8868\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u84dd\u56fe\u8fdb\u884c\u521d\u59cb\u5316<br> &nbsp; &nbsp; &nbsp; &nbsp;for blueprint_path in blueprint_path_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u6bb5\u7684\u5305\u540d\u4f5c\u4e3a\u84dd\u56fe\u7684\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_name: str = blueprint_path.split(\".\")[-1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7ed9\u5f53\u524d\u84dd\u56fe\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a\u84dd\u56fe\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint: Blueprint = Blueprint(blueprint_name, blueprint_path)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5bfc\u5165\u5b50\u8def\u7531\u5173\u7cfb\uff0cblueprint_url_path\u5c31\u662f\u5f53\u524d\u84dd\u56fe\u4e0b\u7684urls\u6a21\u5757\u7684\u5bfc\u5305\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_url_path: str = blueprint_path + \".urls\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urls_module: ModuleType = import_module(blueprint_url_path)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns: List = []<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns = urls_module.urlpatterns<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except Exception:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pass<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5728\u5faa\u73af\u4e2d\uff0c\u628aurlpatterns\u7684\u6bcf\u4e00\u4e2a\u8def\u7531\u4fe1\u606f\u6dfb\u52a0\u6ce8\u518c\u5230\u84dd\u56fe\u5bf9\u8c61\u91cc\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for url in urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint.add_url_rule(**url)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u6700\u540e\u628a\u84dd\u56fe\u5bf9\u8c61\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# todo url_prefix \u662f\u5730\u5740\u524d\u7f00\uff0c\u5c06\u6765\u6211\u4eec\u5c06\u6765\u5b9e\u73b0\u4e00\u4e2a\u603b\u8def\u7531\u6765\u58f0\u660e\u5b83<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.register_blueprint(blueprint, url_prefix=\"\")<br>\u200b<br>def path(rule: str, view_func: Callable, **kwargs):<br> &nbsp; &nbsp;\"\"\"\u7ed1\u5b9aurl\u5730\u5740\u548c\u89c6\u56fe\u7684\u6620\u5c04\u5173\u7cfb\"\"\"<br> &nbsp; &nbsp;return {\"rule\": rule, \"view_func\": view_func, **kwargs}<br>\u200b<\/pre>\n\n\n\n<p>\u5728<code>application.__init__<\/code>\u5165\u53e3\u6587\u4ef6\u4e2d\uff0c\u5bfc\u5165path\u8def\u7531\u6620\u5c04\u51fd\u6570\uff0c\u5c06\u6765\u65b9\u4fbf\u518d\u5176\u4ed6\u5730\u65b9\u8c03\u7528\u8be5\u51fd\u6570\uff0c<code>application.__init__<\/code>\uff0c\u6587\u4ef6\u6dfb\u52a0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from application.utils.blueprint import AutoBluePrint, path<\/pre>\n\n\n\n<p>\u5728home\u84dd\u56fe\u4e0b\u7684urls.py\u4e2d,\u4f7f\u7528path\u51fd\u6570\u7ed1\u5b9a\u89c6\u56fe\u548c\u8def\u7531\u7684\u6620\u5c04\u5173\u7cfb\u5e76\u6dfb\u52a0urlpatterns\u4e2d,<code>home.urls.py<\/code>,\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import List<br>from application import path<br>from . import views<br>\u200b<br>urlpatterns: List = [<br> &nbsp; &nbsp;# path\u7684\u4f5c\u7528\u5c31\u662f\u628a\u4f20\u9012\u7684\u53c2\u6570\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u5b57\u5178\u7ed3\u6784\uff1a{\"rule\": \"\/home\", \"view_func\": views.index, \"methods\": [\"GET\"]},<br> &nbsp; &nbsp;path(\"\/test\", views.test, methods=[\"GET\", \"POST\"]),<br> &nbsp; &nbsp;path(\"\/index\", views.index),<br>]<\/pre>\n\n\n\n<p>\u4e3a\u4e86\u65b9\u4fbf\u6d4b\u8bd5\uff0c\u5148\u5230\u5f53\u524dhome\u84dd\u56fe\u4e0b\u89c6\u56fe\u6587\u4ef6views.py\u4e2d\uff0c\u6dfb\u52a0\u6d4b\u8bd5\u7684\u89c6\u56fe\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def index():<br> &nbsp; &nbsp;return \"home.index\"<br>\u200b<br>def test():<br> &nbsp; &nbsp;return \"home.test\"<br>\u200b<\/pre>\n\n\n\n<p>\u5b8c\u6210\u4e0a\u9762\u6b65\u9aa4\uff0c\u5c06\u6765\u5373\u4fbf\u6709\u65b0\u7684\u84dd\u56fe\u4e5f\u53ef\u4ee5\u53c2\u8003\u4e0a\u9762\u7684\u5199\u6cd5\uff0c\u628a\u5f53\u524d\u84dd\u56fe\u4e2d\u6240\u6709\u7684\u8def\u7531\u5168\u90e8\u96c6\u4e2d\u5728urlpatterns\u4e2d\uff0c\u63a5\u7740\u4e0b\u6765\u6211\u4eec\u5c31\u53ef\u4ee5\u5728<code>application.utils.blueprint.AuthBluePrint<\/code>\u81ea\u52a8\u5316\u84dd\u56fe\u7c7b\u4e2d, \u751f\u6210\u84dd\u56fe\u5bf9\u8c61\u4ee5\u540e\u6dfb\u52a0\u81ea\u52a8\u52a0\u8f7d\u5e76\u6ce8\u518c\u84dd\u56fe\u7684\u8def\u7531\u548c\u89c6\u56fe\u4e86\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6,\u8fd0\u884c\u9879\u76ee\uff0c\u5c31\u53ef\u4ee5\u901a\u8fc7url\u5730\u5740\u8bbf\u95ee\u84dd\u56fe\u4e0b\u7684\u89c6\u56fe\u65b9\u6cd5\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">GET http:\/\/127.0.0.1:5000\/index<br>\u200b<br>POST http:\/\/127.0.0.1:5000\/test<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u4ee3\u7801\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: auto register blueprint\"<br>git push<\/pre>\n\n\n\n<p>\u4e0a\u9762\u84dd\u56fe\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61\u65f6, \u6ca1\u6709\u8bbe\u7f6eurl_prefix\u8def\u7531\u524d\u7f00, \u63a5\u4e0b\u6765\u6211\u4eec\u53ef\u4ee5\u5355\u72ec\u8bbe\u7f6e\u4e00\u4e2a\u603b\u8def\u7531<code>application\/urls.py<\/code>,\u8fdb\u884c\u8def\u7531\u524d\u7f00\u7684\u8bbe\u7f6e.<\/p>\n\n\n\n<p>\u5728\u9879\u76ee\u9ed8\u8ba4\u914d\u7f6e\u6587\u4ef6\u4e2d,<code>application\/settings\/__init__.py<\/code>,\u65b0\u589e\u603b\u8def\u7531\u7684\u914d\u7f6e\u9879URL_PATH<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\"\"\"\u603b\u8def\u7531\"\"\"<br>URL_ROOT_PATH = \"application.urls\"<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765,\u5728<code>application.utils.blueprint<\/code>\u6587\u4ef6\u4e2d\u8c03\u6574path\u51fd\u6570\u4e2d\u63a5\u53d7\u63a5\u53d7\u53c2\u6570\u7c7b\u578b\uff0c\u628a\u8def\u7531\u524d\u7f00\u548c\u84dd\u56fe\u540d\u79f0\u8fdb\u884c\u7ed1\u5b9a\u6620\u5c04\uff0c<code>application.utils.blueprint<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">def path(rule: str, name: Union[Callable, str], **kwargs) -&gt; Dict:<br> &nbsp; &nbsp;\"\"\"\u7ed1\u5b9aurl\u5730\u5740\u548c\u89c6\u56fe\u7684\u6620\u5c04\u5173\u7cfb\"\"\"<br> &nbsp; &nbsp;if isinstance(name, Callable):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"rule\": rule, \"view_func\": name, **kwargs}<br> &nbsp; &nbsp;elif isinstance(name, str):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"url_prefix\": rule, \"blueprint_url_subffix\": name, **kwargs}<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp;return {}<\/pre>\n\n\n\n<p>\u521b\u5efa\u603b\u8def\u7531\u6587\u4ef6\u5e76\u6ce8\u518c\u84dd\u56fe\u548c\u8def\u7531\u524d\u7f00\u7684\u5173\u7cfb, <code>application\/urls.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from typing import List<br>from application import path<br>\u200b<br>\u200b<br>urlpatterns: List = [<br> &nbsp; &nbsp;path(\"\/home\", \"home.urls\"),<br>]<\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u5728\u6ce8\u518c\u84dd\u56fe\u65f6\uff0c\u628a\u8def\u7531\u524d\u7f00\u968f\u7740\u84dd\u56fe\u5bf9\u8c61\u4e00\u8d77\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61\u4e2d\u3002<code>application.utils.blueprint<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from types import ModuleType<br>from typing import List, Optional, Callable, Union, Dict<br>from flask import Flask, Blueprint<br>from importlib import import_module<br>\u200b<br>\u200b<br>class AutoBluePrint(object):<br> &nbsp; &nbsp;def __init__(self, app: Optional[Flask] = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u81ea\u52a8\u6ce8\u518c\u84dd\u56fe<br> &nbsp; &nbsp; &nbsp;  :param app:<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u9700\u8981\u6ce8\u518c\u5230\u9879\u76ee\u4e2d\u7684\u84dd\u56fe\u8def\u5f84\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp;blueprint_path_list: List = app.config.get(\"INSTALL_BLUEPRINTS\", [])<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u603b\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp;url_root_path: str = app.config.get(\"URL_ROOT_PATH\", \"application.urls\")<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u603b\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp;url_root_module: ModuleType = import_module(url_root_path)<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u603b\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp;if not hasattr(url_root_module, \"urlpatterns\"):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message: str = \"\u603b\u8def\u7531\u6587\u4ef6 URL_ROOT_PATH\uff0c\u6ca1\u6709\u8def\u7531\u5217\u8868\uff01\u8bf7\u5728\u603b\u8def\u7531\u6587\u4ef6\u4e2d\u8bbe\u7f6e urlpatterns \u8def\u7531\u5217\u8868\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.logger.error(message)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception(message)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;root_urlpatterns: List = url_root_module.urlpatterns<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u904d\u5386\u84dd\u56fe\u8def\u5f84\u5217\u8868\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u84dd\u56fe\u8fdb\u884c\u521d\u59cb\u5316<br> &nbsp; &nbsp; &nbsp; &nbsp;for blueprint_path in blueprint_path_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u6bb5\u7684\u5305\u540d\u4f5c\u4e3a\u84dd\u56fe\u7684\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_name: str = blueprint_path.split(\".\")[-1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7ed9\u5f53\u524d\u84dd\u56fe\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a\u84dd\u56fe\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint: Blueprint = Blueprint(blueprint_name, blueprint_path)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# # \u5bfc\u5165\u5b50\u8def\u7531\u5173\u7cfb\uff0cblueprint_url_path\u5c31\u662f\u5f53\u524d\u84dd\u56fe\u4e0b\u7684urls\u6a21\u5757\u7684\u5bfc\u5305\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# blueprint_url_path: str = blueprint_path + \".urls\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# urls_module: ModuleType = import_module(blueprint_url_path)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# urlpatterns: List = []<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# try:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; urlpatterns = urls_module.urlpatterns<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# except Exception:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; pass<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# # \u5728\u5faa\u73af\u4e2d\uff0c\u628aurlpatterns\u7684\u6bcf\u4e00\u4e2a\u8def\u7531\u4fe1\u606f\u6dfb\u52a0\u6ce8\u518c\u5230\u84dd\u56fe\u5bf9\u8c61\u91cc\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# for url in urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; blueprint.add_url_rule(**url)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u84dd\u56fe\u8def\u7531\u7684\u524d\u7f00<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url_prefix: str = \"\"<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u84dd\u56fe\u4e0b\u7684\u5b50\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns: List = []<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u7684\u7236\u7ea7\u76ee\u5f55\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u62fc\u63a5\u603b\u8def\u7531\u4e2d\u6240\u6709\u84dd\u56fe\u4e0b\u7684urls\u5b50\u8def\u7531\u6587\u4ef6\u7684\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_father_path: str = \".\".join(blueprint_path.split(\".\")[:-1])<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5faa\u73af\u603b\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for item in root_urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5224\u65ad\u5f53\u524d\u84dd\u56fe\u662f\u5426\u6709\u6ce8\u518c\u5230\u603b\u8def\u7531\u4e2d\u63d0\u4f9b\u5bf9\u5916\u8bbf\u95ee\uff0c\u5982\u679c\u6ca1\u6709\u628a\u84dd\u56fe\u6ce8\u518c\u5230\u603b\u8def\u7531\u4e2d\uff0c\u5219\u65e0\u6cd5\u88ab\u5916\u754c\u8bbf\u95ee\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if blueprint_name in item[\"blueprint_url_subffix\"]:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5bfc\u5165\u5f53\u524d\u84dd\u56fe\u4e0b\u7684\u5b50\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urls_module: ModuleType = import_module(f\"{blueprint_father_path}.{item['blueprint_url_subffix']}\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if hasattr(urls_module, \"urlpatterns\"):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u5b50\u8def\u7531\u6587\u4ef6\u4e2d\u7684\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns: List = urls_module.urlpatterns<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u63d0\u53d6\u84dd\u56fe\u8def\u7531\u7684\u524d\u7f00<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url_prefix = item[\"url_prefix\"]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u628aurlpatterns\u7684\u6bcf\u4e00\u4e2a\u8def\u7531\u4fe1\u606f\u6dfb\u52a0\u6ce8\u518c\u5230\u84dd\u56fe\u5bf9\u8c61\u91cc\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for url in urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint.add_url_rule(**url)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u6700\u540e\u628a\u84dd\u56fe\u5bf9\u8c61\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# url_prefix \u662f\u5730\u5740\u524d\u7f00\uff0c\u5c06\u6765\u6211\u4eec\u5c06\u6765\u5b9e\u73b0\u4e00\u4e2a\u603b\u8def\u7531\u6765\u58f0\u660e\u5b83<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.register_blueprint(blueprint, url_prefix=url_prefix)<br>\u200b<br>\u200b<br>def path(rule: str, name: Union[Callable, str], **kwargs) -&gt; Dict:<br> &nbsp; &nbsp;\"\"\"\u7ed1\u5b9aurl\u5730\u5740\u548c\u89c6\u56fe\u7684\u6620\u5c04\u5173\u7cfb\"\"\"<br> &nbsp; &nbsp;if isinstance(name, Callable):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"rule\": rule, \"view_func\": name, **kwargs}<br> &nbsp; &nbsp;elif isinstance(name, str):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"url_prefix\": rule, \"blueprint_url_subffix\": name, **kwargs}<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp;return {}<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: add blueprint's url_perfix\"<br>git push<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u84dd\u56fe\u6a21\u578b\u7684\u81ea\u52a8\u6ce8\u518c<\/h4>\n\n\n\n<p>\u5728\u84dd\u56fe\u76ee\u5f55home\u4e0b\u7684models.py\u4e2d\u58f0\u660e\u6a21\u578b,\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from application import db<br>\u200b<br>class User(db.Model):<br> &nbsp; &nbsp;__tablename__ = \"ym_user\"<br> &nbsp; &nbsp;id = db.Column(db.Integer, primary_key=True, comment=\"\u4e3b\u952eID\")<br> &nbsp; &nbsp;name = db.Column(db.String(255), unique=True, comment=\"\u8d26\u6237\u540d\")<br> &nbsp; &nbsp;password = db.Column(db.String(255), comment=\"\u767b\u5f55\u5bc6\u7801\")<br> &nbsp; &nbsp;ip_address = db.Column(db.String(255), index=True, comment=\"\u767b\u5f55IP\")<br>\u200b<br> &nbsp; &nbsp;def __repr__(self):<br> &nbsp; &nbsp; &nbsp; &nbsp;return self.name<\/pre>\n\n\n\n<p>\u5b8c\u6210\u4e0a\u9762\u6a21\u578b\u58f0\u660e\u4ee5\u540e\uff0c\u6211\u4eec\u5728\u5165\u53e3\u6587\u4ef6<code>application.__init__<\/code>\u4e2d\u901a\u8fc7 db\u7684 create_all() \u521b\u5efa\u6570\u636e\u8868\u3002<\/p>\n\n\n\n<p><code>application.__init__<\/code>\uff0c\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from pathlib import Path<br>from flask import Flask<br>from flask_sqlalchemy import SQLAlchemy<br>from flask_redis import FlaskRedis<br>from flask_pymongo import PyMongo<br>\u200b<br>from application.utils.config import Config<br>from application.utils.logger import Logger<br>from application.utils.commands import Command<br>from application.utils.blueprint import AutoBluePrint, path<br>\u200b<br>\u200b<br># \u5b9e\u4f8b\u5316\u914d\u7f6e\u52a0\u8f7d\u7c7b<br>config: Config = Config()<br># \u5b9e\u4f8b\u5316SQLAlchemy<br>db: SQLAlchemy = SQLAlchemy()<br># \u5b9e\u4f8b\u5316redis<br>redis_cache: FlaskRedis = FlaskRedis(config_prefix=\"REDIS\")<br>redis_check: FlaskRedis = FlaskRedis(config_prefix=\"CHECK\")<br>\u200b<br># mongoDB\u5b9e\u4f8b\u5316<br>mongo: PyMongo = PyMongo()<br># \u5b9e\u4f8b\u5316\u65e5\u5fd7\u914d\u7f6e\u7c7b<br>logger: Logger = Logger()<br>\u200b<br># \u5b9e\u4f8b\u5316\u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b<br>command: Command = Command()<br>\u200b<br># \u5b9e\u4f8b\u5316\u81ea\u52a8\u5316\u84dd\u56fe\u7c7b<br>blueprint: AutoBluePrint = AutoBluePrint()<br>\u200b<br>def init_app(config_path: str) -&gt; Flask:<br> &nbsp; &nbsp;\"\"\"\u7528\u4e8e\u521b\u5efaapp\u5b9e\u4f8b\u5bf9\u8c61\u5e76\u5b8c\u6210\u521d\u59cb\u5316\u8fc7\u7a0b\u7684\u5de5\u5382\u51fd\u6570\"\"\"<br> &nbsp; &nbsp;# \u5b9e\u4f8b\u5316flask\u5e94\u7528\u5bf9\u8c61<br> &nbsp; &nbsp;app: Flask = Flask(__name__)<br> &nbsp; &nbsp;# \u5168\u5c40\u8def\u5f84\u5e38\u91cf\uff0c\u6307\u5411\u9879\u76ee\u6839\u76ee\u5f55<br> &nbsp; &nbsp;app.BASE_DIR: Path = Path(__file__).resolve().parent.parent<br>\u200b<br> &nbsp; &nbsp;# \u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;config.init_app(app, config_path)<br> &nbsp; &nbsp;# SQLAlchemy\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;db.init_app(app)<br> &nbsp; &nbsp;# redis\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;redis_cache.init_app(app)<br> &nbsp; &nbsp;redis_check.init_app(app)<br> &nbsp; &nbsp;# # pymongo\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;mongo.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u65e5\u5fd7\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;logger.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u7ec8\u7aef\u547d\u4ee4\u7ba1\u7406\u7c7b\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;command.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# \u81ea\u52a8\u5316\u84dd\u56fe\u7c7b\u52a0\u8f7d\u914d\u7f6e<br> &nbsp; &nbsp;blueprint.init_app(app)<br>\u200b<br> &nbsp; &nbsp;# db\u521b\u5efa\u6570\u636e\u8868<br> &nbsp; &nbsp;with app.app_context():<br> &nbsp; &nbsp; &nbsp; &nbsp;db.create_all()<br>\u200b<br> &nbsp; &nbsp;return app<br>\u200b<\/pre>\n\n\n\n<p>\u9879\u76ee\u91cd\u542f\uff0c\u53d1\u73b0flask\u6839\u672c\u4e0d\u77e5\u9053\u6211\u4eec\u5df2\u7ecf\u58f0\u660e\u4e0a\u9762\u7684\u6a21\u578b\uff0c\u6240\u4ee5\u6839\u672c\u6ca1\u6709\u521b\u5efa\u6570\u636e\u8868\uff0c\u8fd9\u4e2a\u539f\u56e0\u662f\uff0c\u6211\u4eec\u6a21\u578b\u5199\u5728\u4e86\u84dd\u56fe\u76ee\u5f55\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u628a\u6a21\u578b\u5bfc\u5165\u5230\u8981\u4e48\u548c\u84dd\u56fe\u5bf9\u8c61\u4e00\u8d77\uff0c\u8981\u4e48\u548capp\u5b9e\u4f8b\u5bf9\u8c61\u4e00\u8d77\u3002\u56e0\u4e3a\u6a21\u578b\u662f\u88ab\u84dd\u56fe\u4fdd\u5b58\u7684\uff0c\u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u5728\u84dd\u56fe\u5bf9\u8c61\u521b\u5efa\u4e2d\uff0c\u5bfc\u5165\u6a21\u578b\uff0c\u8ba9flask\u8bc6\u522b\u5230\u3002<\/p>\n\n\n\n<p><code>application\/utils\/blueprint.py<\/code>,\u4ee3\u7801:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from types import ModuleType<br>from typing import List, Optional, Callable, Union, Dict<br>from flask import Flask, Blueprint<br>from importlib import import_module<br>\u200b<br>\u200b<br>class AutoBluePrint(object):<br> &nbsp; &nbsp;def __init__(self, app: Optional[Flask] = None):<br> &nbsp; &nbsp; &nbsp; &nbsp;if app:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.init_app(app)<br>\u200b<br> &nbsp; &nbsp;def init_app(self, app: Flask):<br> &nbsp; &nbsp; &nbsp; &nbsp;\"\"\"<br> &nbsp; &nbsp; &nbsp;  \u81ea\u52a8\u6ce8\u518c\u84dd\u56fe<br> &nbsp; &nbsp; &nbsp;  :param app:<br> &nbsp; &nbsp; &nbsp;  :return:<br> &nbsp; &nbsp; &nbsp;  \"\"\"<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u9700\u8981\u6ce8\u518c\u5230\u9879\u76ee\u4e2d\u7684\u84dd\u56fe\u8def\u5f84\u4fe1\u606f<br> &nbsp; &nbsp; &nbsp; &nbsp;blueprint_path_list: List = app.config.get(\"INSTALL_BLUEPRINTS\", [])<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u603b\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp;url_root_path: str = app.config.get(\"URL_ROOT_PATH\", \"application.urls\")<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u603b\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp;url_root_module: ModuleType = import_module(url_root_path)<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u603b\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp;if not hasattr(url_root_module, \"urlpatterns\"):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;message: str = \"\u603b\u8def\u7531\u6587\u4ef6 URL_ROOT_PATH\uff0c\u6ca1\u6709\u8def\u7531\u5217\u8868\uff01\u8bf7\u5728\u603b\u8def\u7531\u6587\u4ef6\u4e2d\u8bbe\u7f6e urlpatterns \u8def\u7531\u5217\u8868\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.logger.error(message)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;raise Exception(message)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;root_urlpatterns: List = url_root_module.urlpatterns<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;# \u904d\u5386\u84dd\u56fe\u8def\u5f84\u5217\u8868\uff0c\u5bf9\u6bcf\u4e00\u4e2a\u84dd\u56fe\u8fdb\u884c\u521d\u59cb\u5316<br> &nbsp; &nbsp; &nbsp; &nbsp;for blueprint_path in blueprint_path_list:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u8def\u5f84\u4e2d\u6700\u540e\u4e00\u6bb5\u7684\u5305\u540d\u4f5c\u4e3a\u84dd\u56fe\u7684\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_name: str = blueprint_path.split(\".\")[-1]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u7ed9\u5f53\u524d\u84dd\u56fe\u76ee\u5f55\u521b\u5efa\u4e00\u4e2a\u84dd\u56fe\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint: Blueprint = Blueprint(blueprint_name, blueprint_path)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# # \u5bfc\u5165\u5b50\u8def\u7531\u5173\u7cfb\uff0cblueprint_url_path\u5c31\u662f\u5f53\u524d\u84dd\u56fe\u4e0b\u7684urls\u6a21\u5757\u7684\u5bfc\u5305\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# blueprint_url_path: str = blueprint_path + \".urls\"<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# urls_module: ModuleType = import_module(blueprint_url_path)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# urlpatterns: List = []<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# try:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; urlpatterns = urls_module.urlpatterns<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# except Exception:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; pass<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# # \u5728\u5faa\u73af\u4e2d\uff0c\u628aurlpatterns\u7684\u6bcf\u4e00\u4e2a\u8def\u7531\u4fe1\u606f\u6dfb\u52a0\u6ce8\u518c\u5230\u84dd\u56fe\u5bf9\u8c61\u91cc\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# for url in urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; blueprint.add_url_rule(**url)<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u84dd\u56fe\u8def\u7531\u7684\u524d\u7f00<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url_prefix: str = \"\"<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u84dd\u56fe\u4e0b\u7684\u5b50\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns: List = []<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u84dd\u56fe\u7684\u7236\u7ea7\u76ee\u5f55\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u62fc\u63a5\u603b\u8def\u7531\u4e2d\u6240\u6709\u84dd\u56fe\u4e0b\u7684urls\u5b50\u8def\u7531\u6587\u4ef6\u7684\u8def\u5f84<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint_father_path: str = \".\".join(blueprint_path.split(\".\")[:-1])<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5faa\u73af\u603b\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for item in root_urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5224\u65ad\u5f53\u524d\u84dd\u56fe\u662f\u5426\u6709\u6ce8\u518c\u5230\u603b\u8def\u7531\u4e2d\u63d0\u4f9b\u5bf9\u5916\u8bbf\u95ee\uff0c\u5982\u679c\u6ca1\u6709\u628a\u84dd\u56fe\u6ce8\u518c\u5230\u603b\u8def\u7531\u4e2d\uff0c\u5219\u65e0\u6cd5\u88ab\u5916\u754c\u8bbf\u95ee\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if blueprint_name in item[\"blueprint_url_subffix\"]:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u5bfc\u5165\u5f53\u524d\u84dd\u56fe\u4e0b\u7684\u5b50\u8def\u7531\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urls_module: ModuleType = import_module(f\"{blueprint_father_path}.{item['blueprint_url_subffix']}\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if hasattr(urls_module, \"urlpatterns\"):<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u83b7\u53d6\u5b50\u8def\u7531\u6587\u4ef6\u4e2d\u7684\u8def\u7531\u5217\u8868<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;urlpatterns: List = urls_module.urlpatterns<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u63d0\u53d6\u84dd\u56fe\u8def\u7531\u7684\u524d\u7f00<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;url_prefix = item[\"url_prefix\"]<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u628aurlpatterns\u7684\u6bcf\u4e00\u4e2a\u8def\u7531\u4fe1\u606f\u6dfb\u52a0\u6ce8\u518c\u5230\u84dd\u56fe\u5bf9\u8c61\u91cc\u9762<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for url in urlpatterns:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;blueprint.add_url_rule(**url)<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;try:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u8ba9\u84dd\u56fe\u81ea\u52a8\u53d1\u73b0\u6a21\u578b\u6a21\u5757<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;import_module(f\"{blueprint_path}.models\")<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;except ModuleNotFoundError:<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pass<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# \u6700\u540e\u628a\u84dd\u56fe\u5bf9\u8c61\u6ce8\u518c\u5230app\u5b9e\u4f8b\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# url_prefix \u662f\u5730\u5740\u524d\u7f00\uff0c\u5c06\u6765\u6211\u4eec\u5c06\u6765\u5b9e\u73b0\u4e00\u4e2a\u603b\u8def\u7531\u6765\u58f0\u660e\u5b83<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app.register_blueprint(blueprint, url_prefix=url_prefix)<br>\u200b<br>\u200b<br>def path(rule: str, name: Union[Callable, str], **kwargs) -&gt; Dict:<br> &nbsp; &nbsp;\"\"\"\u7ed1\u5b9aurl\u5730\u5740\u548c\u89c6\u56fe\u7684\u6620\u5c04\u5173\u7cfb\"\"\"<br> &nbsp; &nbsp;if isinstance(name, Callable):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"rule\": rule, \"view_func\": name, **kwargs}<br> &nbsp; &nbsp;elif isinstance(name, str):<br> &nbsp; &nbsp; &nbsp; &nbsp;return {\"url_prefix\": rule, \"blueprint_url_subffix\": name, **kwargs}<br> &nbsp; &nbsp;else:<br> &nbsp; &nbsp; &nbsp; &nbsp;return {}<\/pre>\n\n\n\n<p>\u63d0\u4ea4\u7248\u672c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">git add .<br>git commit -m \"fix: auto find models\"<br>git push<\/pre>\n\n\n\n<p>\u9879\u76ee\u80fd\u81ea\u52a8\u52a0\u8f7d\u603b\u8def\u7531\u4e5f\u80fd\u52a0\u8f7d\u84dd\u56fe\u4e0b\u7684\u5b50\u8def\u7531\u548c\u89c6\u56fe\u6a21\u578b\u4ee5\u540e\u7684\u9879\u76ee\u76ee\u5f55\u7ed3\u6784\uff0c\u5982\u4e0b:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">yingmingapi\/<br>\u251c\u2500\u2500 logs\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u65e5\u5fd7\u76ee\u5f55<br>\u251c\u2500\u2500 applicaton\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u4e3b\u8981\u903b\u8f91\u4ee3\u7801\u4fdd\u5b58\u76ee\u5f55<br> | &nbsp; \u251c\u2500\u2500 settings\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u914d\u7f6e\u5b58\u50a8\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c __init__.py &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u9ed8\u8ba4\u521d\u59cb\u5316\u914d\u7f6e\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c dev.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u5f00\u53d1\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u2514 pro.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u751f\u4ea7\u9636\u6bb5\u7684\u914d\u7f6e\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u521d\u59cb\u5316[\u5168\u5c40\u5165\u53e3\u6587\u4ef6]<br> | &nbsp; \u251c\u2500\u2500 utils\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u9879\u76ee\u5de5\u5177\u7c7b\u5e93\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c blueprint.py &nbsp; &nbsp;  # \u84dd\u56fe\u6ce8\u518c\u76f8\u5173\u7684\u51fd\u6570\u6216\u5de5\u5177\u7c7b<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c commands.py &nbsp; &nbsp; &nbsp; # \u81ea\u5b9a\u4e49\u547d\u4ee4\u548c\u52a0\u8f7d\u547d\u4ee4\u7684\u76f8\u5173\u51fd\u6570<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u251c config.py &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u914d\u7f6e\u52a0\u8f7d\u7684\u8f85\u52a9\u51fd\u6570<br>\u2502&nbsp;&nbsp; \u2502 &nbsp; \u2514 logger.py &nbsp; &nbsp; &nbsp; &nbsp; # \u65e5\u5fd7\u6a21\u5757<br>\u2502&nbsp;&nbsp; \u251c\u2500\u2500 apps\/ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u4fdd\u5b58\u9879\u76ee\u4e2d\u6240\u6709\u84dd\u56fe\u7684\u5b58\u50a8\u76ee\u5f55<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 home &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u84dd\u56fe\u76ee\u5f55\u3010\u8fd9\u91cc\u662f\u4e3e\u4f8b\u800c\u5df2\u3011<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py # \u84dd\u56fe\u7684\u521d\u59cb\u5316\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 urls.py &nbsp; &nbsp; # \u84dd\u56fe\u7684\u5b50\u8def\u7531\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 models.py &nbsp; # \u84dd\u56fe\u7684\u6a21\u578b\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u2514\u2500\u2500 views.py &nbsp;  # \u84dd\u56fe\u7684\u89c6\u56fe\u6587\u4ef6<br>\u2502&nbsp;&nbsp; \u2502&nbsp;&nbsp; \u251c\u2500\u2500 __init__.py<br>\u2502&nbsp;&nbsp; \u2514\u2500\u2500 urls.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u603b\u8def\u7531<br>\u251c\u2500\u2500 manage.py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u9879\u76ee\u7684\u7ec8\u7aef\u7ba1\u7406\u811a\u672c\u6587\u4ef6<br>\u2514\u2500\u2500 command.sh &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  # \u542f\u52a8\u547d\u4ee4<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u521b\u5efa\u865a\u62df\u73af\u5883 conda create -n yingming python=3.8conda activat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19"}],"version-history":[{"count":1,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/22z.top\/index.php?rest_route=\/wp\/v2\/posts\/19\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/22z.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}