簡體   English   中英

Flask render_template 無法顯示引用的模板

[英]Flask render_template not working to show a referenced template

我是 python 和 Flask 的新手,我正在嘗試設置一個基本頁面來呈現 HTML 模板。 我按照說明/教程進行操作,並且可以通過使用 render_template 來渲染 HTML,如下所示:

app = Flask(__name__)

@app.route('/')
def index():
  return render_template(
    'index.html',
  )

我現在正在嘗試運行一個示例應用程序,其中應用程序中的 index.html 實際上引用了 another.html 頁面,如下所示:

../../html/index.html

當我加載這個時,我只看到這個文本 - 從字面上看。 我假設它正在嘗試從引用的文件夾(我驗證存在)加載 index.html 。

這里缺少什么? 我錯過了一個模塊還是什么?

編輯:

這是我正在運行的 main.py 腳本。

在此處輸入圖像描述

index.htmltemplates目錄中。 然后兩個文件夾是它似乎引用的html文件夾:

在此處輸入圖像描述

我知道基於此,它應該是“ ../html/index.html ”,但我也試過了,但這也不起作用。

模板中的 Static 文件應使用url_for function插入,如 Flask 文檔中所述。 你的情況是:

url_for('../../html/index.html')

我想你可以通過兩種方式url_for('templates',filename='index.html') ,其中模板將是 html 文件將被存儲的文件夾,其次你可以使用 jinja2 {% extends index.html %}

暫無
暫無

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

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