python - flask template file not found
問題描述
在同級目錄下,不明白為什么 index.html 和 視圖控制器沒有自動綁定, 提示鴨子類型引起的,是哪里干擾了 視圖的綁定?
那這種,app和 temeplate 在同級目錄,路徑該如何寫?我 寫成 app =Flask(__name__,template_folder=’templates’),pycharm 左側,并沒有提示 ,templates 綁定 html 的圖標
問題解答
回答1:默認情況下模板是在同級目錄templates里的,你這個目錄沒有創建。
回答2:這個主要原因是app在定義的時候,默認是指向templates這個目錄中的,你應該吧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.
要仔細閱讀文檔,里面有寫template_folder參數讀取的目錄是以root_path為準,你要理解每個參數的意義。
回答3:pycharm 設置 Mark as template folder
相關文章:
1. javascript - 移動端,當出現遮罩層的時候,遮罩層里有div是超出高度scroll的,怎么避免滑動div的時候,body跟隨滑動?2. dockerfile - 為什么docker容器啟動不了?3. javascript - 用rem寫的頁面,安卓手機顯示文字是正常的,蘋果顯示的文字是特別小的是為什么呢4. macos - mac下docker如何設置代理5. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應6. javascript - webapp業務流程基本一致,多套主題(樣式基本不一樣,交互稍有偏差)管理,并且有不斷有新增主題,該如何設計組件化架構?7. apache - 本地搭建wordpress權限問題8. 新手 - Python 爬蟲 問題 求助9. javascript - 從mysql獲取json數據,前端怎么處理轉換解析json類型10. javascript - JS設置Video視頻對象的currentTime時出現了問題,IE,Edge,火狐,都可以設置,反而chrom卻...
