discort
discort

Reputation: 728

Get 404 page while load flask app

I've just read about flask.

http://flask.pocoo.org/docs/0.10/quickstart/

First tried to write a small app, worked good. Then I split app to the files and got 404 empty page. Could anyone give me an advice. Where I was wrong.

structure of project:

project/
    application/
        templates/
            main.html
        __init__.py
        views.py
    run.py

file __init__.py

from flask import Flask

app = Flask(__name__)

file run.py

import os
import sys
from application import app

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)

if __name__ == '__main__':
    app.run(debug=True)

file views.py

from flask import render_template
from application import app

@app.route("/")
def index():
    return render_template("main.html")

Upvotes: 0

Views: 565

Answers (1)

Celeo
Celeo

Reputation: 5682

In run.py, you're importing from application import app which brings in your app object from __init__.py. Great!

But that's all it does.

Your views.py file gets the same variable from __init__.py and registers a view. This is what you want to import from run.py:

import os
import sys
from application.views import app

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, PROJECT_DIR)

if __name__ == '__main__':
    app.run(debug=True)

When you impor app from views, views.py pulls app from application. Thus, your run.py gets the app object, but it comes from views.py where it had the route registered.

Upvotes: 1

Related Questions