문제

I have a Flask application that works fine using the development webserver and uwsgi but when deployed to AWS Elastic Beanstalk all of my externally defined routes 404.

I'm using the larger application structure recommended here: http://flask.pocoo.org/docs/patterns/packages/

So I have an application.py that looks like this:

import settings
from flask import Flask

app = Flask(__name__)

app.config.from_object(settings)

application = app

#Ignore not used -> this pulls the views into the main app.
#see http://flask.pocoo.org/docs/patterns/packages/
import messages.views

#more imported applications here

@application.route("/", methods=['GET', 'POST'])
def hello():
    #health check endpoint.
    return "hi"

The hello method within application.py correctly returns 200, but the calls within message.views all return 404s.

도움이 되었습니까?

해결책

As a test I decided to rename my application.py file to app.py and create a new application.py file with the following contents:

from app import app as application

This fixed the issue! I can now access all of my endpoints correctly.

Unfortunately I still don't understand what is wrong with the original setup.

다른 팁

As mentioned in the http://flask.pocoo.org/docs/patterns/packages/ documentation in a flask app architecture like a package you get a directory structure that looks like :

    /yourapplication
        /yourapplication
            /__init__.py
            /static
                /style.css
            /templates
                layout.html
                index.html
                login.html
            /views
                /__init_.py
        ... 
    ...

And you put most of your code withing you __init__.py files so that you can call your views and application as modules of your package.

But as mentioned here you are still using application.py file which doesn't seems to be a part of package. Also While working in a architecture like this you need to be very careful about circular imports as mentioned on the same link near the end of the webpage.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top