I am writing an application with one plan. My application uses Flask-SQLAlchemy, so my project needs access to the main application db(created by Flask-SQLAlchemy) to create its own models.
However, when I try to get the object dbusing current_app.db, the checkbox causes the following error:
RuntimeError: working outside of application context
Here is my main one __init__.py:
from flask import Flask
from app.uploader import uploader
app = Flask(__name__)
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
app.register_blueprint(uploader)
Here __init__.pyfrom my diagram uploader:
from flask import Blueprint
uploader = Blueprint('uploader', __name__,
template_folder='templates')
from . import views
from .models import *
Here's the views.pydrawing in which the exception occurs:
from flask import (redirect, render_template, request, send_from_directory,
session, current_app)
from flask.views import View
from werkzeug import secure_filename
print current_app.db
And here is the stacktrace:
Traceback (most recent call last):
File "runtests.py", line 11, in <module>
import tests
File "/home/plasmasheep/project/tests.py", line 14, in <module>
from app import app, db, user_datastore
File "/home/plasmasheep/project/app/__init__.py", line 6, in <module>
from app.uploader import uploader
File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
from . import views
File "/home/plasmasheep/project/app/uploader/views.py", line 18, in <module>
print current_app.db
File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 338, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/werkzeug/local.py", line 297, in _get_current_object
return self.__local()
File "/home/plasmasheep/project/venv/lib/python2.7/site-packages/flask/globals.py", line 34, in _find_app
raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context
Just an attempted use from .. import dbdoes not work:
Traceback (most recent call last):
File "runtests.py", line 11, in <module>
import tests
File "/home/plasmasheep/project/tests.py", line 14, in <module>
from app import app, db, user_datastore
File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
from app.uploader import uploader
File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
from . import views
File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
from .. import db
ImportError: cannot import name db
Doesn't from app import db:
Traceback (most recent call last):
File "runtests.py", line 11, in <module>
import tests
File "/home/plasmasheep/project/tests.py", line 14, in <module>
from app import app, db, user_datastore
File "/home/plasmasheep/project/app/__init__.py", line 7, in <module>
from app.uploader import uploader
File "/home/plasmasheep/project/app/uploader/__init__.py", line 6, in <module>
from . import views
File "/home/plasmasheep/project/app/uploader/views.py", line 17, in <module>
from app import db
ImportError: cannot import name db