How to use PyMongo with Flask Blueprints?

前端 未结 2 2035
我在风中等你
我在风中等你 2021-02-19 19:00

What is the correct way of picking up mongo object inside Blueprints?

Here is how I have my parent login.py:

app.config.from_object(\         


        
2条回答
  •  感情败类
    2021-02-19 19:26

    You're are initializing the PyMongo driver twice, once in child.py and a second time on child2.py.

    Try initializing the PyMongo connection in the file which sets up your app object and then import it in the children:

    login.py:

    app.config.from_object('config')
    from flask.ext.pymongo import PyMongo
    from child import child
    from child2 import child2
    
    
    app = Flask(__name__)
    mongo = PyMongo(app)
    
    # Register blueprints
    def register_blueprints(app):
        # Prevents circular imports
        app.register_blueprint(child2.child2)
        app.register_blueprint(child.child)
    
    register_blueprints(app)
    

    in child.py

    from app import app, mongo
    
    child = Blueprint('child', __name__)
    

    child2.py:

    from app import app, mongo
    
    child2 = Blueprint('child2', __name__)
    

提交回复
热议问题