gpt4 book ai didi

python - 部署在 Apache 上时,使用带有相对路径的 webpy 的 web.template.render()

转载 作者:行者123 更新时间:2023-11-28 21:30:06 25 4
gpt4 key购买 nike

使用 webpy,引用 web.template.render() 的模板目录的正确方法是什么,以便它可以在 webpy 开发 Web 服务器和 Apache 上运行?

以下代码可以在开发服务器上运行,但在我的 Apache 服务器上运行时不能运行。

import web

urls = (
'/', 'index',
)

class index:
def GET(self):
render = web.template.render('templates/')
return render.index(self)

我知道问题出在 web.template.render('templates/') 上,因为当 Apache 从 C:\Program 运行时,相对路径不再有效文件\Apache Software Foundation\Apache2.2。我的模板目录位于我的项目文件夹中。

我不想做的是使用绝对路径,因为我希望能够移动我的项目文件,而不必修改代码以保持其正常工作。

最佳答案

如果您使用 mod_wsgi,最简单的解决方案是设置 home= option适本地,

或者,您可以获取模块的路径并将其与模板结合起来,即

os.path.join(os.path.dirname(__file__), 'templates/')

如果您经常需要它,请将其放入函数中。请注意,如果您将其放在单独的模块中,则该模块需要与 templates 目录位于同一文件夹中,否则您将再次得到错误的目录。

如果您想将其放入系统范围的包中,您可以轻松找到调用者目录:

def abspath(path): 
frame = sys._getframe(1)
base = os.path.dirname(frame.f_globals['__file__'])
return os.path.join(base, path)

关于python - 部署在 Apache 上时,使用带有相对路径的 webpy 的 web.template.render(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3697704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com