python - flask template file not found
問(wèn)題描述
在同級(jí)目錄下,不明白為什么 index.html 和 視圖控制器沒(méi)有自動(dòng)綁定, 提示鴨子類(lèi)型引起的,是哪里干擾了 視圖的綁定?
那這種,app和 temeplate 在同級(jí)目錄,路徑該如何寫(xiě)?我 寫(xiě)成 app =Flask(__name__,template_folder=’templates’),pycharm 左側(cè),并沒(méi)有提示 ,templates 綁定 html 的圖標(biāo)
問(wèn)題解答
回答1:默認(rèn)情況下模板是在同級(jí)目錄templates里的,你這個(gè)目錄沒(méi)有創(chuàng)建。
回答2:這個(gè)主要原因是app在定義的時(shí)候,默認(rèn)是指向templates這個(gè)目錄中的,你應(yīng)該吧index.html放到rest1的templates目錄中。或者在app=Flask(__name__, template_folder=’path’), 指定路徑http://flask.pocoo.org/docs/0... 此為Flask API文檔
Parameters: import_name – the name of the application packagestatic_url_path – can be used to specify a different path for the static files on the web. Defaults to the name of the static_folder folder.static_folder – the folder with static files that should be served at static_url_path. Defaults to the ’static’ folder in the root path of the application.template_folder – the folder that contains the templates that should be used by the application. Defaults to ’templates’ folder in the root path of the application.instance_path – An alternative instance path for the application. By default the folder ’instance’ next to the package or module is assumed to be the instance path.instance_relative_config – if set to True relative filenames for loading the config are assumed to be relative to the instance path instead of the application root.root_path – Flask by default will automatically calculate the path to the root of the application. In certain situations this cannot be achieved (for instance if the package is a Python 3 namespace package) and needs to be manually defined.
要仔細(xì)閱讀文檔,里面有寫(xiě)template_folder參數(shù)讀取的目錄是以root_path為準(zhǔn),你要理解每個(gè)參數(shù)的意義。
回答3:pycharm 設(shè)置 Mark as template folder
相關(guān)文章:
1. 前端 - Web內(nèi)部網(wǎng)頁(yè)登陸控制,怎么通過(guò)apache控制?還有其他的控制嗎?2. javascript - table固定尾行,有人寫(xiě)過(guò)嗎?3. java - C++ 編譯器選擇問(wèn)題4. java - 如何理解“不要通過(guò)共享內(nèi)存來(lái)通信,而應(yīng)該通過(guò)通信來(lái)共享內(nèi)存”?5. javascript - 使用node.js的ws模塊不斷地向客戶(hù)端發(fā)送消息,應(yīng)該如何不斷地去觸發(fā)“發(fā)送”這個(gè)動(dòng)作6. css - 網(wǎng)頁(yè)div區(qū)塊 像蘋(píng)果一樣可左右滑動(dòng) 手機(jī)與電腦7. nginx - 如何根據(jù)服務(wù)器配置設(shè)置web服務(wù)參數(shù)?8. 為什么學(xué)習(xí)PHP9. javascript - 如何在NW.JS中使用Node自動(dòng)重啟進(jìn)程?10. 引入traits后,為什么index得是空的呢?
