2015-09-28 39 views
0

我有一個文件test1.py。我正在使用web.py將其顯示在網頁中。在python web.py中插入圖像

import web 

    urls = ('/', 'hello','/bye', 'bye') 

    app = web.application(urls, globals(), True) 

    class hello: 
     def GET(self): 
     return """<html> 
     <head> 
     </head> 
      <body> 
      <img src="smile.png" alt="SAP Logo" width="500px" height="100px"/> 
      </body></html>""" 

然而,在運行時,它將顯示:

127.0.0.1:59558 - - [28/Sep/2015 15:52:32] "HTTP/1.1 GET /" - 200 OK 
127.0.0.1:59558 - - [28/Sep/2015 15:52:32] "HTTP/1.1 GET smile.png" - 404 Not Found 

圖像被放置在桌面上。

+0

如果圖像放置在桌面上,是否意味着服務器的文件夾根目錄也在桌面上? – Enermis

+0

你在哪裏告訴web.py如何從桌面上提供圖像? –

回答

2

在您的項目根目錄下創建一個「靜態」文件夾,將您的「smile.png」複製到該文件夾​​中。現在改變img標籤的src值:

<img src="/static/smile.png" alt="SAP Logo" width="500px" height="100px"/> 

您應該看到您smile.png在布勞爾現在,對於在web.py提供靜態文件的詳細信息,就往「http://webpy.org/cookbook/staticfiles」。