2

我在烧瓶应用程序中有 3 个蓝图,目录结构如下:

main/
   __init__.py
   books/
   users/
   authors/
   apps/

每个包 inisdemain都是一个blueprint. 在我的main/__init__.py我有

from flask import Flask
from flask_pymongo import PyMongo

app = Flask(__name__)

from main.users.views import users
from main.admin.views import admin
app.register_blueprint(users, url_prefix='/api/users')

MONGO_HOST = os.environ['MONGO_HOST']
MONGO_PORT = os.environ['MONGO_PORT']

app.config["MONGO_URI"] = "mongodb://{}:{}/".format(MONGO_HOST, MONGO_PORT)
mongo = PyMongo(app)

如何在每个蓝图中访问 mongo?这是在这里使用 mongo 的正确方法吗?在官方文档中,它说不要使用类似的东西db=Pymongo(app)

4

1 回答 1

0

我想答案对你来说来得太晚了,但最终它还是会有所帮助。

我通常排除外部文件中的数据库行,例如database.py。然后分别在我的应用程序和蓝图中导入mongo实例。请考虑以下示例。为了完成和理解,我还添加了其他对函数有意义的元素。

数据库.py

from flask_pymongo import PyMongo
mongo = PyMongo()

表格.py

from wtforms import Form
from wtforms.fields import BooleanField, PasswordField, StringField
from wtforms.validators import Email, Required

class LoginForm(Form):
    email = StringField('Email', validators=[Required(), Email('Not a valid email address')])
    password = PasswordField('Password',validators=[Required()])
    remember = BooleanField('Remember')

身份验证.py

from flask import Blueprint, redirect, render_template, request
from flask_login import LoginManager, UserMixin, current_user, login_user
from werkzeug.security import check_password_hash
from database import mongo
from forms import LoginForm

authentication = Blueprint('authentication', __name__, template_folder='templates')

login_manager = LoginManager()
login_manager.login_view = 'authentication.log_in'

@authentication.route('/login', methods=['GET', 'POST'])
def log_in():
    if (current_user.is_authenticated):
        return redirect(url_for('get_index'))

    login_form = LoginForm(request.form)

    if (request.method == 'POST'):
        if (login_form.validate()):
            user_collection = mongo.db.user
            user = user_collection.find_one({'email':login_form.email.data})

            if (user) and (check_password_hash(user['password'],
                            login_form.password.data)):
                login_user(User(user), login_form.remember)
                return redirect(url_for('get_index'))
            else:
                return render_template('login.html', form=login_form)

    elif (request.method == 'GET'):
        return render_template('login.html', form=login_form)

class User(UserMixin):

    def __init__(self, user):
        super()
        self.id = user['_id']
        self.email = user['email']

    def get(user_id, mongo):
        user_collection = mongo.db.user
        user = user_collection.find_one({'_id':ObjectId(user_id)})
        return User(user)


@login_manager.user_loader
def load_user(user_id):
    return User.get(user_id, mongo)

应用程序.py

from flask import Flask, render_template, request
from flask_login import login_required

from authentication import authentication, login_manager
from database import mongo
from forms import LoginForm

app = Flask(__name__)
app.secret_key = os.urandom(24)

app.config['MONGO_URI'] = 'mongodb:27017/app'
mongo.init_app(app)

app.register_blueprint(authentication)
login_manager.init_app(app)

@app.route('/', methods=['GET'])
@login_required
def get_index():
    if (request.method == 'GET'):
        render_template('index.html')

if __name__ == '__main__':
    app.run(host="0.0.0.0")
于 2020-04-20T13:37:21.460 回答