gpt4 book ai didi

Python Bottle 框架错误 500 : can't find template in Daemon mode

转载 作者:太空狗 更新时间:2023-10-30 02:30:29 25 4
gpt4 key购买 nike

我目前正在使用 bottle framework 在 Python 中开发一个简单的网络应用程序.这是我的应用程序结构:

结构

lib
- bottle.py
- bottledaemon.py
- lockfile.py
- __init__.py
view
- dashboard.tpl
run.py

这是我的 run.py 代码:

#!/usr/bin/env python
from lib.bottle import route, template, run, debug, request, static_file
from lib.bottledaemon import daemon_run

debug(mode=True)

@route('/')
def show_index():

return template('dashboard')

# If the following line is enabled, the server will start in non-Daemon mode.
#run(host='0.0.0.0', port=80, debug=True)

# If the following lines are enabled, the server will start in Daemon mode.
if __name__ == "__main__":
daemon_run()

所以我希望 WSGI 服务器通过将其传递给 bottle daemon script 在守护进程中运行.

问题

当运行非守护进程的代码时,它可以工作。它向我显示了正确的模板,并且在 CLI 中我可以看到 HTTP 请求。

但是,当我在守护进程模式下运行相同的代码时,它确实作为守护进程启动,因此工作正常,但它再也找不到模板了。它向我显示此错误消息:

Error: 500 Internal Server Error

Sorry, the requested URL 'HERE IS MY WEBSITE URL' caused an error:

Template 'template' not found.

因此,当我以守护进程模式启动网络服务器时,似乎无法再找到 .tpl 文件的文件路径。我已经尝试了很多东西,但我无法弄清楚,我想保持路径动态。大家有什么建议吗?

谢谢!

最佳答案

这可能是一个路径问题,我能够重新创建它并通过手动将 View 文件夹的路径添加到 bottles TEMPLATE_PATH 列表来修复它。

from bottle import route, template, run, debug, request, static_file, TEMPLATE_PATH
from bottledaemon import daemon_run

import os

TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))

# rest of script

编辑:

追根究底,肯定是路径问题。 bottledaemon 导入 daemon 并运行 DaemonContext,默认情况下将工作目录更改为 '/'bottledaemon 没有像它应该的那样覆盖它。因此,当 bottle 查找 view 文件夹的相关路径时,它实际上是在系统根目录中查找“/view”。

关于Python Bottle 框架错误 500 : can't find template in Daemon mode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26630466/

25 4 0