我真的很难让这个工作,我希望你能帮助我。
我的项目如下所示:
文件夹结构
/app
/auth
views.py
/user
views.py
__init__.py
config.py
run.py
run.py
from app import create_app
app = create_app()
app/__init__.py
(处理应用程序的启动)
from .auth.views import app as app_auth
from .user.views import app as app_user
from flask import Flask
from flask_mysqldb import MySQL
from .config import Config
db = MySQL() # database instance
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
# force database config
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
with app.app_context():
# init db
db.init_app(app)
# register blueprints
app.register_blueprint(app_auth, url_prefix="/auth")
app.register_blueprint(app_user, url_prefix="/u")
return app
app/user/views.py
(示例视图)
from flask import Blueprint, render_template, request, current_app
app = Blueprint('user', __name__)
######################################################
# HOW CAN I ACCESS THE DB IN THIS FILE?
######################################################
@app.route("/")
def sample():
return "nothing to display yet"
简而言之:我怎样才能访问数据库views.py
?
此致