Python Forum

Full Version: [Flask] No application found.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've looked around the web and got all the code from a tutorial I have been going through, but one of my lines isn't working. It is a problem with the query. If I, on debug mode on the website, try Post.query I get "RuntimeError: No application found. Either work inside a view function or push an application context." I can't figure out the problem thanks in advance for the help.
Error comes from second line
page = request.args.get('page', 1, type=int)
posts = Post.query.order_by(Post.date_posted.desc()).paginate(page=page, per_page=5)
run.py (file I run in the command line to start website)
from FlaskBlog import create_app

app = create_app()
app.app_context().push()

if __name__ == '__main__':
    app.run(debug=True)
Here is the create_app funcion in the __init__ file for FlaskBlog
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(Config)

    db.init_app(app)
    bc.init_app(app)
    login_manager.init_app(app)
    mail.init_app(app)

    from FlaskBlog.users.routes import usersApp
    from FlaskBlog.posts.routes import postsApp
    from FlaskBlog.main.routes import mainApp
    app.register_blueprint(usersApp)
    app.register_blueprint(postsApp)
    app.register_blueprint(mainApp)

    return app
If you need it - a tree of the files
FlaskBlog
    |
    |--main
    |   |--__init__.py
    |   |--routes.py
    |   
    |--posts
    |   |--__init__.py
    |   |--forms.py
    |   |--routes.py
    |
    |--static
    |   |--a css file and directory full of pictures
    |
    |--Templates
    |    |--A ton of html templates
    |
    |--users
    |   |--__init__.py
    |   |--forms.py
    |   |--utils.py
    |   |--routes.py
    |
    |--__init__.py
    |--config.py
    |--models.py
    |--site.db
run.py
I put the tree in a code block so it would maintain the spaces
If it helps, I get a different error from the website, the error I named above is when i try to query Post from the website debugger. The error I get is -
AttributeError: 'NoneType' object has no attribute 'drivername'
The last lines of code are -
The default implementation provides some saner defaults for things
        like pool sizes for MySQL and sqlite.  Also it injects the setting of
        `SQLALCHEMY_NATIVE_UNICODE`.
        """
        if sa_url.drivername.startswith('mysql'): #This line gives the error
            sa_url.query.setdefault('charset', 'utf8')
            if sa_url.drivername != 'mysql+gaerdbms':
                options.setdefault('pool_size', 10)
                options.setdefault('pool_recycle', 7200)
        elif sa_url.drivername == 'sqlite':

I tried deleting my database and then running "db.create_all()" but I got the same error - "No application found"

I'm thinking I will just start over