【问题标题】:How to get cherrypy to serve static files from the index and a static directory, with custom paths as well?如何让cherrypy从索引和静态目录中提供静态文件,以及自定义路径?
【发布时间】:2018-10-18 21:56:31
【问题描述】:

我的目录结构如下所示:

project/
    index/
        about.html
        index.html
        forum.html
        profile.html
        settings.html
        apple-touch-icon.png
        static/
            main.css
            forum.css
            main.js
            forum.js
            load-image.min.js
    server.py
    metaclass.py
    mailing.py
    errors.log

我希望能够让cherrypy 提供来自index/ 的所有这些文件。但是,我也希望通过/about//forum/profile 等可以访问about.htmlindex.htmlforum.htmlprofile.html 等,所以这不是与简单的静态文件服务相同。另外,我想要一些自定义方法,比如/login,它需要GETPOST,以及预先模板化的用户配置文件页面。如何做到这一点?

【问题讨论】:

标签: python cherrypy static-content


【解决方案1】:

Cherrypy 将递归地提供索引文件夹中的文件。您正在尝试做的更多的是与 url 路径有关。

在您的 server.py 中,您可以附加 /about.html 的处理程序以实现您想要的。

    @cherrypy.expose
    def about_html(self):
       return open('/index/about.html')

希望这会有所帮助!

【讨论】: