Usually in Flask
flask.py
from flask import Flask
app = Flask(__file__)
If you define your application this way, but want to make it with ** MVT **,
blueprint.py
from flask import Blueprint
app = Blueprint("app", __name__, url_prefix="/path")
Define the application in this way.
For example
.
├── app.py
├── model.py
├── static
│ ├── interface.js
│ └── layout.css
├── template
│ ├── design.html
│ └── main.html
└── view
├── __init__.py
├── api.py
├── auth.py
├── main.py
└── setting.py
For such a file structure.
** Define the application with Blueprint for each Python script under view /
. ** **
api.py
from flask import Blueprint
app = Blueprint("api", __name__, url_prefix="/api")
You need to register the Blueprint of each Python script under ** view /
in ʻapp.py`. ** **
app.py
from flask import Flask
from view import api, auth, main, setting
application = Flask(__name__)
modules_define = [api.app, auth.app, main.app, setting.app]
for app in modules_define:
application.register_blueprint(app)
First, import each Python script under the view /
directory.
from view import api, auth, main, setting
Then ** register the Blueprint application **
application.register_blueprint(app)
Now you can do MVT.
By the way, instead of registering the Blueprint application under view /
directly in ʻapp.py, register the Blueprint application in
init.pyunder
view / and register it in ʻapp.py
. There is also a method of importing with.
__init__.py
from flask import Flask
import api, auth, main, setting
application = Flask(__name__)
modules_define = [api.app, auth.app, main.app, setting.app]
for app in modules_define:
application.register_blueprint(app)
app.py
from view import application
This may be easier.
Recommended Posts