【问题标题】:Bottle not routing to index html file瓶子没有路由到索引 html 文件
【发布时间】:2018-12-02 21:05:28
【问题描述】:

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

该项目在 TA 的机器上运行良好,但在我的笔记本电脑上,bottle 没有路由到索引页面,即使我明确让它从 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 包都已更新,目前我没有想法。您的建议/建议将不胜感激。谢谢。

【问题讨论】:

  • 可能是路径问题。您可以从hello 中打印 cwd 吗?
  • 'hello' 中的 cwd?你能澄清你的问题吗?我对 Python 比较陌生。

标签: python bottle


【解决方案1】:

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

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

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

【讨论】:

  • 对于“Python:当前文件(集成终端)”,它被配置为:{“name”:“Python:当前文件(集成终端)”,“type”:“python”, "request": "launch", "program": "${file}", "console": "integratedTerminal", "stopOnEntry": false, "host": "localhost" } 我不认为集成 shell 是在其他目录中执行代码,因为除了静态文件夹之外没有其他文件,它只有 index.html 文件。我还在文件夹之外放置了一个副本,但最终结果仍然是错误 500。
猜你喜欢
  • 1970-01-01
  • 2019-04-10
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多