Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my view function 404?

Tags:

python

flask

Directory Structure:

__init__:

from flask import flask 

app = Flask(__name__)


if __name__ == '__main__'
    app.run()

Views:

from app import app

@app.route('/')
def hello_world():
    return 'Hello World!'

I hope someone can explain what I am doing wrong here - I guess I'm not understanding how to properly import app. This results in a 404. However when views is moved back to __init__ everything works properly.

like image 393
Shilo Avatar asked Jul 12 '15 00:07

Shilo


People also ask

Why does 404 keep appearing?

You'll get 404 errors if you've deleted or removed pages from your site recently without redirecting their URLs. 404 errors can also occur if you've relaunched or transferred your domain and failed to redirect all your old URLs to the new site. Sometimes 404 errors can be the result of changing a page's URL.


1 Answers

You need to explicitly import your views module in your __init__:

from flask import flask 

app = Flask(__name__)
from . import views    

Without importing the module, the view registrations are never made.

Do keep the script portion outside of your package. Add a separate file in Final_app (so outside the app directory) that runs your development server; say run.py:

def main():
    from app import app
    app.run()

if __name__ == '__main__'
    main()
like image 50
Martijn Pieters Avatar answered Sep 22 '22 15:09

Martijn Pieters