【问题标题】:Is there a way to use relative paths while serving static files using CherryPy 3.2 with a config file?有没有办法在使用带有配置文件的 CherryPy 3.2 提供静态文件时使用相对路径?
【发布时间】:2011-09-07 15:47:21
【问题描述】:

我最近问了这个相关的问题: Problems serving static files favicon.ico and robots.txt in CherryPy 3.1

在我的配置文件中,我描述了一个绝对路径。有没有办法让它成为相对路径?原因是我在一个团队中。我的队友和我的服务器使用不同的路径来定位我们的代码。

[/]
tools.staticdir.on = True
tools.staticdir.root = "/projects/mysite/trunk/root"
tools.staticdir.dir = ""
tools.staticfile.root = "/projects/mysite/trunk/root"

[/favicon.ico]
tools.staticfile.on = True
tools.staticfile.filename = "images/favicon.ico"

[/robots.txt]
tools.staticfile.on = True
tools.staticfile.filename = "robots.txt"

[/images]
tools.staticdir.on = True
tools.staticdir.dir = "images"

[/css]
tools.staticdir.on = True
tools.staticdir.dir = "css"

[/js]
tools.staticdir.on = True
tools.staticdir.dir = "js"

【问题讨论】:

  • 相对于起始 python 文件所在的位置。加载此配置文件的 Python 文件。

标签: python cherrypy


【解决方案1】:

我才开始使用 Cherrypy,因此可能有充分的理由不这样做,但以下是我添加到 .conf 文件中以从静态目录提供文件的内容。注意我在执行python程序的时候是在static上面的目录中。

[/]
tools.staticdir.root = os.getcwd()

[/static]
tools.staticdir.on = True
tools.staticdir.dir = "static"

【讨论】:

    【解决方案2】:

    这行得通吗?

        'tools.staticdir.root': os.path.join(os.path.abspath(os.curdir), 'trunk/root'),
    

    【讨论】:

    猜你喜欢
    • 2020-07-31
    • 2020-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2012-01-02
    • 1970-01-01
    • 2012-05-09
    • 2011-07-22
    相关资源
    最近更新 更多