flask - easy application but with error 404 not found
You have two problems:
- In
views/index.py
you don't actually defineapp
, so that will result in aNameError
if you ever actually importviews.index
. - In
__init__.py
you never importviews.index
so your route never gets added to theFlask.url_routes
map.
You have two options:
You can take the circular imports way out, as specified in the docs:
# views.indexfrom flask import render_templatefrom domain import app@app.route("/")def index(): return render_template("index.html")# __init__.py# ... snip ...db = SQLAlchemy(app)# View imports need to be at the bottom# to ensure that we don't run into problems # with partially constructed dependencies# as this is a circular import# (__init__ imports views.index which imports __init__ which imports views.index ...)from views import index
You can pull the creation of
app
into a separate file and avoid circular imports entirely:# NEW: infrastructure.pyfrom flask import Flaskfrom flask.ext.sqlalchemy import SQLAlchemyapp = Flask("domain")db = SQLAlchemy(app)# views.indexfrom domain.infrastructure import app# NEW: app.pyfrom domain.infrastructure import appimport domain.views.index# __init__.py# is now empty
You need import views.index in domain/init.py, and add "from domain import app" in index.py. Otherwise it cannot find app