flask - easy application but with error 404 not found flask - easy application but with error 404 not found flask flask

flask - easy application but with error 404 not found


You have two problems:

  1. In views/index.py you don't actually define app, so that will result in a NameError if you ever actually import views.index.
  2. In __init__.py you never import views.index so your route never gets added to the Flask.url_routes map.

You have two options:

  1. 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
  2. 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