1

I tried for about 5 hours now to display a image to my webpage. My code is like this:

import os
import cherrypy

file_path = os.getcwd().replace("\\", "/")

class Home:
    def index(self):
        return """<img src="img/logolong.jpg" alt="Main Page" />"""
    index.exposed = True

if __name__ == "__main__":
    cherrypy.server.socket_host = "127.0.0.1"
    cherrypy.server.socket_port = 50505
    config = {"/static":
                        {"tools.staticdir.on": True,
                         "tools.staticdir.dir": file_path,
                        },
                      "/images":
                        {"tools.staticdir.on": True,
                        "tools.staticdir.dir": file_path+"/img"}
                    }

    cherrypy.tree.mount(Home(), "/", config=config)
    cherrypy.engine.start()
    cherrypy.engine.block()

I searched everywhere and tried like everything. I'm using Google Chrome and Windows7.

Thanks a lot for help!

4

2 回答 2

1

您的代码在 URI 处公开 logolong.jpg /static/images/img/logolong.jpg,但您的 HTML 说它应该在/img/logolong.jpg. 您需要以一种或另一种方式调和这些。要保持 HTML 原样并修复配置,您应该将其更改为:

config = {
    "/img": {
        "tools.staticdir.on": True,
        "tools.staticdir.dir": os.path.join(file_path, "img"),
    }
}

os.path.join以这种方式使用意味着您可以摆脱replace("\\", "/")上述情况)

这将在URI处提供文件%cd%\\img\\logolong.jpg /img/logolong.jpg

于 2012-09-14T14:33:29.590 回答
0

尝试更改此行...

return """<img src="img/logolong.jpg" alt="Main Page" />"""\

到这条线...

return """<img src="/logolong.jpg" alt="Main Page" />"""

安德鲁

于 2012-09-14T13:24:49.010 回答