python - 瓶子没有路由到索引 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 包都已更新,目前我没有想法。您的建议/建议将不胜感激。谢谢你。
解决方案
问题可能是因为集成的 shell 在文件“index.html”所在的其他目录中执行代码。
为了帮助解决问题,请将文件名替换为绝对路径,例如
@route('/static/')
def hello():
return static_file(os.path.join(os.path.dirname(__file__), 'index.html'), root='static')
推荐阅读
- java - 抛出“异常A:异常B”时如何断言“异常B”?
- python-3.x - 分割图像的重叠预测
- firebase - 使用 auth-rules 调用 firestore
- javascript - 使用 API axios 从 React 中一次性删除 mongoDB 中的所有数据
- spring-boot - Thymeleaf 形式 - 如何将模型属性的值从一个控制器方法传递到另一个控制器方法
- django - 将只读副本分配给 Django 中的特定应用程序?
- google-apps-script - 如何在自动创建后将电子表格的 URL 引用复制到不同谷歌电子表格中的跟踪表
- java - 为什么 java.exe 由于“org.xnio.nio”线程而消耗更多 CPU?
- amazon-web-services - 从用户数据设置默认 AWS 区域
- linux - 如何解析脚本中差异的结果?