Flask deployment to Heroku: 404 URL Not Found, discussing Flask file structure compatible for Heroku

拈花ヽ惹草 提交于 2020-03-05 03:14:12

问题


I've gotten past the error mentioned here I think... But there's an error 404 message that won't go away for my flask application once I launch it on heroku, and I'm not sure what to do at this point. Heroku/Flask deployment failing, either 'can't find attribute app' or 'can't find module name (flask_module)', proc file to blame?

I restructured my flask project so that it uses a "single module" organizational pattern.

File tree:

My __init__.py and experiment.py/app.py files

On my previous question, I was told that the way I wrote these was "weird" so...

My experiment.py file. I included a flask() call at the top of the file based on other posts and code I saw.

import functools

from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for, Flask
)
from werkzeug.security import check_password_hash, generate_password_hash


app = Flask(__name__, instance_relative_config=True) # creates the flask instance

bp= Blueprint('experiment', __name__)

###
# Some view functions...
###

if __name__ == "__main__":

    app.run()

__init__.py is pretty much borrowed straight from Flask's quickstart tutorial, but I removed the Flask() call and import the variable assigned to that, app, from the experiment.py file.

import os

from flask import Flask
from flask import (
    Blueprint, flash, g, redirect, render_template, request, session, url_for
)

# app = Flask(__name__, instance_relative_config=True)

def create_app(test_config=None):

    from .experiment import app
    app.config.from_mapping(
        SECRET_KEY='dev',
        DATABASE=os.path.join(app.instance_path, 'Neuroethics_Behavioral_Task.sqlite'),
    )

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    app.register_blueprint(experiment.bp)
    app.add_url_rule('/', endpoint='index')


    return app

Procfile

The last and important part of this is my Procfile, which I'm still not sure of how it's written at this point.

heroku ps:scale web=1

web: gunicorn experiment:app

Note: this application still isn't online, I just made an important observation though.

I tried switching from a "package" organizational pattern to a "single module" organizational pattern since, the vast majority of tutorials use a single module pattern. That means moving all the contents of app_dir to the root directory.

Then I had my procfile include the following: web: gunicorn experiment:app.

This changed the error. Instead, an unstylized html page loaded saying that "the url was not found". checking heroku logs showed the following

2020-01-30T21:47:14.127902+00:00 heroku[web.1]: State changed from starting to up
2020-01-30T21:47:13.808735+00:00 app[web.1]: [2020-01-30 21:47:13 +0000] [4] [INFO] Starting gunicorn 20.0.4
2020-01-30T21:47:13.828081+00:00 app[web.1]: [2020-01-30 21:47:13 +0000] [4] [INFO] Listening at: http://0.0.0.0:24870 (4)
2020-01-30T21:47:13.830362+00:00 app[web.1]: [2020-01-30 21:47:13 +0000] [4] [INFO] Using worker: sync
2020-01-30T21:47:13.850689+00:00 app[web.1]: [2020-01-30 21:47:13 +0000] [10] [INFO] Booting worker with pid: 10
2020-01-30T21:47:13.878989+00:00 app[web.1]: [2020-01-30 21:47:13 +0000] [11] [INFO] Booting worker with pid: 11
2020-01-30T21:47:14.000000+00:00 app[api]: Build succeeded
2020-01-30T21:47:23.319429+00:00 heroku[router]: at=info method=GET path="/" host=neuroethics-behavioral-task.herokuapp.com request_id=46d28f97-a46a-479d-b56f-0ad910e85f03 fwd="144.118.77.114" dyno=web.1 connect=0ms service=32ms status=404 bytes=385 protocol=https
2020-01-30T21:47:23.318447+00:00 app[web.1]: 10.155.88.71 - - [30/Jan/2020:21:47:23 +0000] "GET / HTTP/1.1" 404 232 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

The last part is the most important,

2020-01-30T21:47:23.319429+00:00 heroku[router]: at=info method=GET path="/" host=neuroethics-behavioral-task.herokuapp.com request_id=46d28f97-a46a-479d-b56f-0ad910e85f03 fwd="144.118.77.114" dyno=web.1 connect=0ms service=32ms status=404 bytes=385 protocol=https
2020-01-30T21:47:23.318447+00:00 app[web.1]: 10.155.88.71 - - [30/Jan/2020:21:47:23 +0000] "GET / HTTP/1.1" 404 232 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

So this error has status 404. Also it doesn't crash the same way that heroku normally crashes for a Module Not Found error like before.

Taking a look at this post... 404 Error when running Flask app on Heroku

I think I'm gonna stay with my single module organizational pattern for my application since it's what most people use.

What I have observed that seems key to me is that... if I comment out the lines in my __init__.py file for both registering a blueprint and adding a url rule, then my flask application locally produces the same unstylized error 404 page that I always see on heroku.

So I'm stuck with this error 404, but now I'm finally growing suspicious that the issue is related to urls not being properly mounted for some reason once I deploy the application to heroku.

A key question I have at this point... is my experiment.py/app.py file supposed to make a call to create_app(), and is this somehow not getting executed when deployed to heroku? Is my experiment.py file also supposed to add url rules/blueprints? If I try to copy the two lines from __init__.py that do this, then my app doesn't launch locally.

来源:https://stackoverflow.com/questions/59994288/flask-deployment-to-heroku-404-url-not-found-discussing-flask-file-structure-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!