Search code examples
pythonpython-3.xflask

Flask views in separate module


flaskr.py

# flaskr.py    
from flask import Flask


app = Flask(__name__)

import views


if __name__ == "__main__":
    app.run()

views.py

# views.py
from flaskr import app
from flask import render_template, g

@app.route('/')
def show_entries():
    entries = None
    return render_template('show_entries.html', entries=entries)

python3 flaskr.py

Can anyone tell me why this isn't working but if i move the whole app into a separate package it works flawless.

No errors, not nothing except a 404 like the views.py is ignored. I'm knew to Flask and i'm trying out different things to understand how it actually works.

Thanks!


Solution

  • Apparently, this has to do with app.root_path.

    • In views.py, app.root_path is /path/to/project/flaskr
    • But in flaskr.py, app.root_path is /path/to/project

    So Flask expects views.py to be put into a package.