提交 c63ce1dd 编写于 作者: checklate's avatar checklate

项目初始化:配置文件+项目启动+路由蓝本

上级 8ff2ee7a
# first_blog # first_blog
参考资料:《Flask Web开发:基于Python的Web应用开发实战(第2版)》 参考资料:《Flask Web开发:基于Python的Web应用开发实战(第2版)》
\ No newline at end of file
启动服务:
1、终端中设置环境变量`$env:FLASK_APP = "flasky.py"`
2、使用flask run启动
\ No newline at end of file
...@@ -6,3 +6,16 @@ ...@@ -6,3 +6,16 @@
# Author: GaoNingNing # Author: GaoNingNing
# Date: 2022/11/27 # Date: 2022/11/27
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
from flask import Flask
from config import config
def create_app(config_name):
app = Flask(__name__)
app.config.from_object(config[config_name])
config[config_name].init_app(app)
# 添加主路由
from .main import main as main_blueprint
app.register_blueprint(main_blueprint)
return app
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: __init__.py
# Description: 应用主模块
# Author: GaoNingNing
# Date: 2022/11/27
# -------------------------------------------------------------------------------
from flask import Blueprint
# 主蓝本
main = Blueprint('main', __name__)
from . import views
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: views
# Description: 视图函数
# Author: GaoNingNing
# Date: 2022/11/27
# -------------------------------------------------------------------------------
from app.main import main
@main.route('/')
def index():
return '<h1>Hello World!</h1>'
...@@ -6,3 +6,47 @@ ...@@ -6,3 +6,47 @@
# Author: GaoNingNing # Author: GaoNingNing
# Date: 2022/11/27 # Date: 2022/11/27
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config:
"""
配置基类
"""
pass
@staticmethod
def init_app(app):
pass
class DevelopmentConfig(Config):
"""
开发环境配置
"""
DEBUG = True
class TestingConfig(Config):
"""
测试环境配置
"""
TESTING = True
class ProductionConfig(Config):
"""
生产环境配置
"""
pass
config = {
'development': DevelopmentConfig,
'testing': TestingConfig,
'production': ProductionConfig,
'default': DevelopmentConfig
}
...@@ -6,3 +6,8 @@ ...@@ -6,3 +6,8 @@
# Author: GaoNingNing # Author: GaoNingNing
# Date: 2022/11/27 # Date: 2022/11/27
# ------------------------------------------------------------------------------- # -------------------------------------------------------------------------------
import os
from app import create_app
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册