2017-02-26 72 views
2

Python代碼:Jinja2的模板沒有找到和內部服務器錯誤

from flask import Flask, render_template 

app = Flask(__name__) 

@app.route("/") 

def hello(): 

    return render_template('testing.html') 

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

HTML文件:

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<h1>My name is pk</h1> 
</body> 
</html> 

而且如何啓用pycharm社區版本的Jinja2。我直接在

|-app 
|--templates // where your html files must be in 
|--static // where your js and css files must be in 
|--.py files 
|--other packages 

而且神社在你的系統中啓用,如果您已經下載燒瓶包在同一個項目

+0

,你能否告訴我們完整的錯誤,以及你是如何構建你的文件和目錄? – coralvanda

+0

確保您的'testing.html'文件位於模板文件夾內。如果你能展示你的應用結構,那會更好。 – thangtn

+0

@coralvanda我通過使用下面提到的策略得到了解決方案。謝謝,請繼續回答我的問題。 – pk786

回答

2

默認情況下,Flask將在應用程序根級別的templates文件夾中查找。

http://flask.pocoo.org/docs/0.10/api/

template_folder - 包含應 應用程序中使用的模板的文件夾。默認爲應用程序路徑的根目錄 中的'templates'文件夾。

所以,你有一些選擇,

  1. 重命名templatetemplates
  2. 供應template_folder參數去有你template文件夾中的燒瓶應用的認可:

    app = Flask(__name__, template_folder='template') 
    

燒瓶預計templates目錄與創建它的模塊位於相同的文件夾中; 你需要告訴瓶到別處尋找替代:

app = Flask(__name__, template_folder='../pages/templates') 

這個工程作爲路徑是相對於當前模塊路徑解決。

你不能讓每個模塊模板目錄,如果不使用blueprints。一種常見的模式是使用templates文件夾的子目錄來替代您的模板。你會使用templates/pages/index.html,滿載render_template('pages/index.html')

+0

非常感謝。這幫助了我。 – pk786

2

燒瓶文件結構創建HTML文件和.py文件。

+0

謝謝,我剛剛檢查了文件結構中存在一些問題。現在工作完美。 – pk786