Flask蓝图

必要的结构目录

.
├── manage.py ## 启动文件
└── weixin
    ├── admin
    │   ├── __init__.py
    │   └── views.py
    └── __init__.py
  1. 首先在weixin/_init_.py 创建一个flask对象
from flask import Flask
app = Flask(__name__)
  1. 接下来在admin/_init_.py引入蓝图对象
from flask import Blueprint

admin = Blueprint("admin", __name__)

from . import views
  1. 回到weixin/_init_.py ,注册刚刚创建admin蓝图的对象
from flask import Flask
app = Flask(__name__)

from .admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin') # url_prefix是访问蓝图的链接前缀如:www.abc.com/admin
  1. admin/views.py中可以写试图函数,如一个简单的hello world
from . import admin


@admin.route("/")
def index():
    return "Hello World!"
  1. manage.py作为启动文件
from weixin import app

if __name__ == "__main__":
    app.run(debug=True)

你可能感兴趣的:(Flask)