【问题标题】:Python Bottle framework error 500: can't find template in Daemon modePython Bottle 框架错误 500:在守护进程模式下找不到模板
【发布时间】:2014-12-25 03:56:24
【问题描述】:

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

结构

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 请求。

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

错误:500 内部服务器错误

抱歉,请求的 URL 'HERE IS MY WEBSITE URL' 导致错误:

找不到模板“模板”。

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

谢谢!

【问题讨论】:

    标签: python wsgi bottle


    【解决方案1】:

    这可能是路径问题,我能够通过手动将视图文件夹的路径添加到瓶子 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 不会像它应该覆盖的那样覆盖。所以当瓶子寻找view 文件夹的相对路径时,它实际上是在系统的根目录上寻找'/view'。

    【讨论】:

    • 回溯(最近一次调用最后):文件“run.py”,第 6 行,在 TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os. path.dirname(_file _), "view"))) NameError: name 'TEMPLATE_PATH' is not defined
    • 看最上面一行,你需要把它添加到你的导入列表中
    • 谢谢伙计!现在守护程序再次启动。仍然是相同的 500 错误。我必须在您提供的代码中插入静态路径吗?
    • 不应该,奇怪的是你提供的错误说它正在寻找'Template 'template',而你指定dashboard。我建议将 dashboard.tpl 重命名为 template.tpl 以查看是否暂时显示。
    • 非常感谢您解决了这个问题!我被困了几个小时。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多