0

I am trying to get flask-login to work with blueprints and factory, and am not getting it to work (after a lot of searching and trying).

This i how i try to make it work:

bp_home = Blueprint('bp_home', __name__)
bp_waterlevels = Blueprint('bp_waterlevels', __name__)


def create_app():
    app = Flask(__name__)
    app.register_blueprint(bp_home)
    app.register_blueprint(bp_waterlevels)
    # Include configuration file handling
    configure_app(app)

    return app

login_manager = LoginManager()
login_manager.init_app(create_app())
login_manager.login_view = 'login'

@login_manager.user_loader
def load_user(username):
    return User(username)


@bp_home.before_request
def before_request():
    g.user = current_user

The error i have is :

AttributeError: 'Flask' object has no attribute 'login_manager'

I know it has to do with the load order of the login_manager, but i cannot figure it out.

NOTE : I have seen this ; How do i handle login in flask with multiple blueprints question but it is not working for me. The use of factory gives problems with finding out how to fix the loading order.

Can somebody point me in the right direction, i am kind of stuck ...

Peter
  • 57
  • 5
  • I suspect you should be setting the login manager inside your factory. I'm not really a fan of the factory pattern here. I just create my flask app & do setup in my `server.py` and then load my blueprints from there. – Wayne Werner Apr 11 '16 at 20:26
  • I have seen the answer , but it is not using the factory pattern and did not work for me. And Wayne, i have tried other methods, but got a lot of other problems with blueprint, flask_login etc. Thanks anyway, cheers! – Peter Apr 11 '16 at 20:37
  • Did you see [this answer](http://stackoverflow.com/a/36318204/344286)? Is there any particular reason that you feel the factory pattern is any better than just making an app? – Wayne Werner Apr 11 '16 at 20:44
  • One of the problems is that i want to have a view file per blueprint, this gave me a lot of headaches with circulair loading of "app". I tried a lotof things but could get around that. The other reason was that because of circuilair loading i could not use app.config['some var'] in eg models.py. When i had one view file everything was working fine, but i moved into something i cannot grasp and did not found a sufficient answer for, despite a lot of reading and trying. I really would like to use Flask with blueprint, flask-login and flask-menu, etc and move on to something more productive :) – Peter Apr 11 '16 at 20:52
  • You're supposed to use `current_app` in the blueprint. Always. – Wayne Werner Apr 11 '16 at 21:00
  • I found that out Wayne, i will refacter and try to figure it out , but first i have a lot of reading to do on the subject. Thanks – Peter Apr 12 '16 at 15:19
  • I have refactored my flask app and found a solution without the factory. – Peter Apr 19 '16 at 07:13

0 Answers0