2017-06-17 179 views
0

新來燒瓶,並有一些python的經驗,當使用render_template時,它不會呈現模板,也不會給出任何錯誤。 代碼這裏:燒瓶render_template

from flask import Flask, render_template 

app = Flask(__name__, template_folder= "/templates") 




@app.route("/") 
def index(): 
     #return("Index str") 
    return render_template("index.html") 


@app.route("/crawler") 
def crawler(): 
    return("WebCrawler str") 
    return render_template("crawler.html") 

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

HTML這裏(很肯定的文件層次是正確的)。

<!DOCTYPE html> 

<html lang="en"> 
    <head> 
     <link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}"> 
     <meta charset = "utf-8"> 
     <title> Index</title> 
    <head> 
    <body> 
     <p> This webserver hosts all webapps and will showcase any unfinished applications. 
      </p> 

     </body> 

</html> 

root virtual environment

static for css

templates folder

回答

2

爲HTML文件的默認文件夾是模板。因此,在您的python文件所在的位置創建一個名爲「模板」的文件夾。將任何HTML文件放在該文件夾中

+0

最後一個屏幕截圖顯示了main.py所在的文件夾中的模板文件夾。 – Hobnob

+0

你是否試圖通過本地主機訪問該網站?如果是這樣,那麼你需要導入操作系統並具有以下代碼:'if __name__ =='__main__': port = int(os.environ.get('PORT',5000)) app.run(host ='0.0。 0.0',port = port)' –

+0

事實上,我從來沒有想過,我只是現在就測試一下。 – Hobnob

0

默認瓶template_folder設置爲templates。如果你想改變它,然後在應用程序的根路徑中提到文件夾。

It't時不必templates文件夾,但它應該是...

app = Flask(__name__, template_folder= "templates") 
+0

我已經嘗試將其保留爲默認值,但這也不起作用,如果需要,我可以顯示歸檔的屏幕截圖嗎? – Hobnob

+0

@Hobnob那會很好。請更新您的問題與提交截圖... –

+0

你可以把'app.debug = True'在'app.run()'語句之上。它會給你更好的錯誤信息... –