Ganesh Shenoy
Ganesh Shenoy

Reputation: 629

Python flask URL mapping issue, returning 404

-->project
    --->run.py
    --->config.py  
    --->readme.md 
    --->app
        --->__init__.py
        --->controllers
            --->__init__.py
            --->test_controller.py
        --->model
            --->__init__.py
            --->test_model1.py
            --->test_model2.py

run.py

from app import app
app.run(host = '0.0.0.0', port = 8080, debug = True)

config.py - All configuration variable

app/__init__.py

from flask import Flask
app = Flask(__name__)

controllers/__init__.py - Empty

controllers/test_controller.py

@app.route('/test', methods=['POST'])
    def test():
    return "Hello world"

When I start my server form run.py the server gets started.
But when I try the URL http://locahost:8080/test, it returns 404.
But if the route is configured in app/___init__.py it is working.

Can anyone please guide me what is incorrect here in configuration.
I want to keep the above structure, please let me know of any issues.

Upvotes: 0

Views: 264

Answers (1)

MatsLindh
MatsLindh

Reputation: 52832

Unless you import the file containing the @app.route decorator, it won't be registered. Flask won't import and register all .py files automagically for you.

At the end of your __init__.py file in app/, import projectname.controllers, and import test_controller in the __init__.py file in the controllers module.

Upvotes: 1

Related Questions