2014-02-13 157 views
1

我有幾個不同的模板,我試圖用於我的燒瓶應用程序。燒瓶:render_template與路徑

我曾嘗試以下,但它似乎只能看裏面直接/模板,而不是/模板/資料夾,模板/資料夾等

return render_template('index.html', template_folder='folder1') 
return render_template('folder1/index.html') 

預期都沒有工作,我怎麼能指定不同模板的子文件夾。

+1

第二個應該只是罰款 - 那麼你的目錄結構是什麼樣子? –

+0

裏面的模板文件夾我有'folder1'包含index.html和'fol​​der2'包含index2.html – KJW

回答

-1

我覺得肖恩是對的,還有:你嘗試過雙引號嗎?我使用的是藍圖,因此它可能是不同的,但是這是我的樣子:

return render_template("users/register.html") 

所以你可能是:

return render_template("folder1/index.html") 
+3

以任一種方式的單引號,雙引號或三引號......它們都是字符串,因此沒有差別。 :-) –

+1

我是herp,我是derp。 –

+0

沒關係我用的是index2.html而不是index.html – KJW

0

的模板文件夾可以在創建應用程序瓶時指定(或藍圖):

from flask import Flask 
app = Flask(__name__, template_folder='folder1') 

來源:http://flask.pocoo.org/docs/0.12/api/#application-object

from flask import Blueprint 
auth_blueprint = Blueprint('auth', __name__, template_folder='folder1') 

來源:http://flask.pocoo.org/docs/0.12/blueprints/#templates

template_folder是相對於應用程序/藍圖所在的位置。 使用os庫創建路徑到app/blueprint目錄之外的模板文件夾。

例如。從應用程序根目錄的子目錄

import os 
APP_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 
TEMPLATE_PATH = os.path.join(APP_PATH, 'templates/') 
  • 運行
  • APP_PATH檢索父目錄路徑(應用程序根)