2015-04-02 125 views
0

我得到一個內部服務器錯誤,我不知道是什麼問題。 index.html與Python文件位於同一目錄中。爲什麼這個簡單的應用程序返回500錯誤?

from flask import Flask, render_template 
app = Flask(__name__) 

@app.route('/') 
def hello_world(): 
    author = "Rick" 
    name = "1st flask app" 
    return render_template('index.html', author=author, name=name) 

if __name__ == "__main__": 
    app.run() 
<!DOCTYPE html> 
<html> 
    <head> 
    <title>{{ author }}'s app</title> 
    </head> 

    <body> 
    <h2>Hello {{ name }}!</h2> 
    <p>Please show up on the page</p> 
    </body> 
</html> 

爲什麼我收到一個500錯誤,而不是我的渲染模板?

+1

請在您的問題中包含來自控制檯輸出或日誌文件的追蹤。 – 2015-04-02 16:33:46

回答

3

創建一個名爲templates的目錄,並將index.html放入其中。當您創建的應用程序 app = Flask(__name__)

app.run(debug=True) 
1

還可以包括template_folder參數指定包含您的模板文件夾:

您可以通過在調試模式下運行獲得有關錯誤的詳細信息。如果沒有,render_template將在名爲templates的默認文件夾中查找它們。如果這樣的文件夾不存在,或者您的模板無法在其中找到,您將得到內部服務器錯誤500,而不是您的呈現模板。

相關問題