首页 > 解决方案 > 瓶子没有路由到索引 html 文件

问题描述

我将 Bottle 用于 Python 项目,并将项目设置为在 localhost:8080(Windows 10 笔记本电脑)上运行。我已经在 VS Code 上对项目进行了编码,但是,当我在 VS Code 的集成终端上启动调试器时,我的浏览器(Google Chrome)上出现错误 500。

该项目在 TA 的机器上运行良好,但是在我的笔记本电脑上,即使我明确让它从 static_file 导入并将根文件添加到“运行”功能,瓶子也没有路由到索引页面。我尝试从 Bottlepy.org 运行示例,即使这样也不起作用。

唯一有效的是:

from bottle import run, route

@route('/')
def hello():
   return "If you're seeing this message, then bottle is working"
run(host='localhost', port=8080)

再一次,我跑了:

from bottle import run, route, template

@route('/')
def hello():
   return template("index.html")
run(host='localhost', port=8080)

from bottle import run, route, static_file

@route('/static/')
def hello():
  return static_file('index.html', root='static')
run(host='localhost', port=8080)

包括来自 bottlepy.org 的示例,结果如下:

Error 500 Template 'index.html' not found.

或者

Error 500 ‘Template ‘/’ not found

我不认为这是 Python 的 PATH 问题,但它可能是 VS 代码的 JSON 文件问题。我机器上的所有 Python 包都已更新,目前我没有想法。您的建议/建议将不胜感激。谢谢你。

标签: pythonbottle

解决方案


问题可能是因为集成的 shell 在文件“index.html”所在的其他目录中执行代码。

为了帮助解决问题,请将文件名替换为绝对路径,例如

@route('/static/')
def hello():
  return static_file(os.path.join(os.path.dirname(__file__), 'index.html'), root='static')

推荐阅读