title: Using Flask-User with app factory and blueprints
body: <p>I am working on my first bigger Flask App and having a few hang-ups with my project structure.</p>
  
  <p>My App has the usual <code>models.py, routes.py</code> and <code>forms.py</code>. In the base folder I have <code>my_app.py</code> which imports <code>create_app</code> from <code>app</code>. I organized <code>routes.py</code> and <code>forms.py</code> inside a blueprint and register that blueprint in <code>create_app()</code> inside <code>__init__.py</code>. </p>
  
  <p>As I have multiple roles, I am using <code>Flask-User</code> and need a <code>UserManager</code> object. To instanciate it, I need the app, db and user. Because <code>routes.py</code> is a blueprint which is registered with the app I tried to instanciate <code>Usermanager</code> with the blueprint. Gets me a Type Error.<br>
  Where do I instanciate the UserManager, where I also have the User model or how do I get my instance of <code>app</code> inside a module?</p>
  
  <p><strong>EDIT:</strong> I have found that there is <code>current_user</code> for my problem stated above.</p>
  
  <p>Another hickup I have is in <code>forms.py</code>. I am generating one form with data from the db. When I do the <code>Questions.query.all()</code> I get a <code>RuntimeError: No application found. Either work inside a view function or push an application context.</code> 
  What am I doing wrong there?</p>
  
  <p>I even get that error inside the <code>create_all()</code> function when I try </p>
  
  <pre><code>from app import models
  db.create_all()
  </code></pre>
  
  <p>.</p>
  
  <pre><code>├── app
  │   ├── __init__.py
  │   ├── main
  │   │   ├── __init__.py
  │   │   ├── forms.py
  │   │   └── routes.py
  │   ├── models.py
  │   └── templates
  │       ├── admin
  │       │   └── index.html
  │       ├── base.html
  │       ├── index.html
  ├── config.py
  ├── my_app.py
  </code></pre>
  
  <p><code>init.py</code></p>
  
  <pre><code>from flask import Flask
  from config import Config
  from flask_sqlalchemy import SQLAlchemy
  from flask_login import LoginManager
  from flask_user import UserManager
  from flask_babelex import Babel
  
  db = SQLAlchemy()
  login = LoginManager()
  
  def create_app():
      app = Flask(__name__)
      app.config.from_object(Config)
      db.init_app(app)
      login.init_app(app)
  
      from app.main import bp as main_bp
      app.register_blueprint(main_bp)
  
  from app import models
  
  # Setup Flask-User and specify the User data-model
  user_manager = UserManager(app, db, models.User)
  </code></pre>
  
  <p><code>routes.py</code></p>
  
  <pre><code>from flask import render_template
  from app.main.forms import LoginForm, QuestionForm, SurveyForm, TokenForm
  from app.models import Question, Answer, User
  from app.main import bp
  from app.models import db
  from flask_login import login_user, logout_user
  from flask_user import current_user, login_required, roles_required, UserManager, UserMixin
  
  # Setup Flask-User and specify the User data-model
  user_manager = UserManager(bp, db, User)
  
  @bp.route('/')
  @bp.route('/index')
  @roles_required('Admin')
  def index():
      return render_template('index.html')
  </code></pre>
  
  <p><code>forms.py</code></p>
  
  <pre><code>from flask_wtf import FlaskForm
  from wtforms import $Fields...
  from app.models import Question
  from app import db
  
  def getQuestions():
      '''Get questions from db to generate forms'''
      questions = Question.query.all()
  </code></pre>