简体   繁体   中英

404 Error when running Flask app on Heroku

I'm attempting to build a very simple Flask application using just Flask and Python. No databases or other plugins. The application runs perfectly fine on my local machine and I can navigate without errors, but when I deploy to Heroku, I receive a 404 Not Found error for any page that I try to visit, including the index.

Here's my site's layout: Site Layout

mbtaCrTracker.py is the root of my app. Here's what it currently looks like:

from mbtaCrTracker import app
from mbtaCrTracker import views
app.run(host='0.0.0.0', debug=True)

Here's my _ init _.py:

from flask import Flask
app = Flask(__name__)

Here's my views.py:

from flask import render_template, request, redirect
from mbtaCrTracker import app
from getTrainInfo import getTrainInfo

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

@app.route('/map', methods=['GET'])
def findTrain():
    line = request.args.get('line')
    direction = request.args.get('direction')
    coords = getTrainInfo(line, direction)
    return render_template('map.html', coords=coords)

Here's my Procfile:

web: gunicorn mbtaCrTracker:app --log-file=-

I think everything is set up properly because I'm able to run the app locally without any issues. When I deploy to Heroku, I can see that the site is Up, not Crashed, and I'm not receiving any server errors, just Not Found errors. It's almost as if my routes aren't working or something like that.

Here's what I see in my Heroku logs when I try to access my site's homepage:

2017-11-06T12:23:17.717474+00:00 heroku[web.1]: Starting process with command `gunicorn mbtaCrTracker:app --log-file=-`
2017-11-06T12:23:19.795333+00:00 app[web.1]: [2017-11-06 12:23:19 +0000] [4] [INFO] Starting gunicorn 19.7.1
2017-11-06T12:23:19.796263+00:00 app[web.1]: [2017-11-06 12:23:19 +0000] [4] [INFO] Listening at: http://0.0.0.0:43494 (4)
2017-11-06T12:23:19.796405+00:00 app[web.1]: [2017-11-06 12:23:19 +0000] [4] [INFO] Using worker: sync
2017-11-06T12:23:19.800312+00:00 app[web.1]: [2017-11-06 12:23:19 +0000] [8] [INFO] Booting worker with pid: 8
2017-11-06T12:23:19.810431+00:00 app[web.1]: [2017-11-06 12:23:19 +0000] [9] [INFO] Booting worker with pid: 9
2017-11-06T12:23:21.351354+00:00 heroku[web.1]: State changed from starting to up
2017-11-06T12:23:29.564508+00:00 heroku[router]: at=info method=GET path="/" host=mbtacrtracker.herokuapp.com request_id=cdae33d9-9f9d-4568-b619-b6c10bb34e9b fwd="172.58.216.112" dyno=web.1 connect=1ms service=19ms status=404 bytes=386 protocol=https  

Thoughts?

Here are some steps you may follow to successfully deploy on Heroku:

Rename mbtaCrTracker.py to app.py

Cut the contents of mbtaCrTracker to root directory. Delete the mbtaCrTracker directory.

app.py should contain:

from flask import Flask, render_template, request, url_for, redirect
from getTrainInfo import getTrainInfo

app = Flask(__name__)

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

@app.route('/map', methods=['GET'])
def findTrain():
    line = request.args.get('line')
    direction = request.args.get('direction')
    coords = getTrainInfo(line, direction)
    return render_template('map.html', coords=coords)

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

Procfile contains:

web: gunicorn app:app

And one simple way to deploy this is to push this in github and then link the github repository with the app in heroku .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM