簡體   English   中英

在Heroku上運行Flask應用時出現404錯誤

[英]404 Error when running Flask app on Heroku

我試圖僅使用Flask和Python構建一個非常簡單的Flask應用程序。 沒有數據庫或其他插件。 該應用程序可以在本地計算機上完美運行,並且可以無錯誤地進行導航,但是當我部署到Heroku時,我嘗試訪問的任何頁面(包括索引)都收到404 Not Found錯誤。

這是我的網站布局: 網站布局

mbtaCrTracker.py是我的應用程序的根目錄。 這是當前的樣子:

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

這是我的_ init _.py:

from flask import Flask
app = Flask(__name__)

這是我的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)

這是我的Procfile:

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

我認為一切都已正確設置,因為我能夠在本地運行該應用程序而沒有任何問題。 當我部署到Heroku時,可以看到該站點已啟動,而不是崩潰,並且我沒有收到任何服務器錯誤,只是未找到錯誤。 幾乎就像我的路線不起作用一樣。

這是我嘗試訪問網站主頁時在Heroku日志中看到的內容:

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  

有什么想法嗎?

您可以按照以下步驟在Heroku上成功部署:

mbtaCrTracker.py重命名為app.py

mbtaCrTracker的內容mbtaCrTracker到根目錄。 刪除mbtaCrTracker目錄。

app.py應該包含:

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包含:

web: gunicorn app:app

部署它的一種簡單方法是將其推送到github ,然后將github存儲庫與heroku的應用程序鏈接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM