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. docker api 开发的端口怎么获取?2. node.js - 我是一个做前端的,求教如何学习vue,node等js引擎?3. docker images显示的镜像过多,狗眼被亮瞎了,怎么办?4. 前端 - 类到底该如何去命名 .newsList 这种的命名难道真的不是过度语义化吗?~5. 如何解决Centos下Docker服务启动无响应,且输入docker命令无响应?6. java - 为什么第一个线程已经释放了锁,第二个线程却不行?7. 如何解决docker宿主机无法访问容器中的服务?8. PHP中的$this代表当前的类还是方法?9. sublime-text - sublime text 3中编译python(sublimeREPL),如何仅运行单行/选定部分10. 老师,flex-shrink: 1; 按视频操作,不会自动缩放

网公网安备