2015-01-01 70 views
0

我迷失在python和flask中,因爲我不明白路由結構如何在HTML頁面之間導航。hwo我可以使用正確的路由來顯示Flask的HTML頁面嗎?

部署到Heroku的我的結構是:

/my_app_name/ 
app.py 
config.py 
drivers.html 
Procfile 
requirements.txt 

的app.py文件是:

import os 
from flask import Flask, render_template, request, url_for 
from flask.ext.sqlalchemy import SQLAlchemy 
import json 


app = Flask(__name__) 
db = SQLAlchemy(app) 

app = Flask(__name__) 

@app.route('/') 
def hello(): 
    return 'Hello World from Python!' 

@app.route('/') 
def drivers(): 
    drivers = db.drivers.select() 
    return render_template('drivers.html') 
    #return HttpResponse('/drivers.html', json.dumps(result), content_type='application/json') 

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

config.py是:

SQLALCHEMY_DATABASE_URI = 'heroku_database_uri_string) #connection is OK, no problem here 

drivers.html是:

{% block body %} 
    <ul> 
    {% for driver in drivers %} 
    <li><h2>{{ driver.driver_name }}</h2> 
    </ul> 
{% endblock %} 

當我瀏覽到myapp.heroku.com/我「從pyhton世界你好」得很好的,但是當我瀏覽到myapp.heroku.com/drivers.html

,我收到了「404」的錯誤。

所以,2個問題:

1)爲什麼404?哪裏不對?

2)我認爲是,但整個結構是否有缺陷?

非常感謝!

回答

0

你肯定一定已經注意到這些行:

@app.route(...) 
在你的代碼

。他們定義路線。你有兩個處理程序指向「/」,而大概你想讓驅動程序處理程序指向「/ drivers」:所以改變你傳遞給route的參數。

此外,你不應該考慮HTML頁面,你不應該試圖給你的路線「.html」後綴。只需使用「驅動程序」,並導航到myapp.heroku.com/drivers。

最後,沒有必要上傳到Heroku的測試這樣的:只需運行本地開發服務器。請注意,所有這些在Flask文檔中都有詳細描述;你應該閱讀它們。

+0

感謝這部分幫助我。我已閱讀燒瓶文檔,但尚未完全理解它們。毫無疑問,我會回來尋求更多幫助! – user1903663

相關問題