登录  /  注册
python - flask template file not found
ringa_lee
ringa_lee 2017-06-22 11:52:00
[Python讨论组]

在同级目录下,不明白为什么 index.html 和 视图控制器没有自动绑定
, 提示鸭子类型引起的,是哪里干扰了 视图的绑定?

那这种,app和 temeplate 在同级目录,路径该如何写?
我 写成 app =Flask(__name__,template_folder='templates'),
pycharm 左侧,并没有提示 ,templates 绑定 html 的图标

ringa_lee
ringa_lee

ringa_lee

全部回复(3)
扔个三星炸死你

这个主要原因是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 package
static_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为准,你要理解每个参数的意义。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号