title: How to Display Image with web.py
body: <p>I am trying to let a user upload an image, save the image to disk, and then have it display on a webpage, but I can't get the image to display properly. Here is my <code>bin/app.py</code>:</p>
  
  <pre><code>import web                                                                         
  
  urls = (                                                                           
          '/hello', 'index'                                                          
          )                                                                          
  
  app = web.application(urls, globals())                                             
  
  render = web.template.render('templates/', base="layout")                          
  
  class index:                                                                       
      def GET(self):                                                                 
          return render.hello_form()                                                 
  
      def POST(self):                                                                
          form = web.input(greet="Hello", name="Nobody", datafile={})                
          greeting = "%s, %s" % (form.greet, form.name)                                                                
          filedir = 'absolute/path/to/directory'
          filename = None                                                            
          if form.datafile:                                                     
              # replaces the windows-style slashes with linux ones.                  
              filepath = form.datafile.filename.replace('\\','/')                    
              # splits the and chooses the last part (the filename with extension)
              filename = filepath.split('/')[-1]                                     
              # creates the file where the uploaded file should be stored            
              fout = open(filedir +'/'+ filename,'w')                                
              # writes the uploaded file to the newly created file.                  
              fout.write(form.datafile.file.read())                                  
              # closes the file, upload complete.                                    
              fout.close()                                                           
              filename = filedir + "/" + filename                                    
          return render.index(greeting, filename)                                    
  
  if __name__ == "__main__":                                                         
      app.run()     
  </code></pre>
  
  <p>and here is <code>templates/index.html</code>:</p>
  
  <pre><code>$def with (greeting, datafile)                                                     
  
  $if greeting:                                                                      
      I just wanted to say &lt;em style="color: green; font-size: 2em;"&gt;$greeting&lt;/em&gt;
  $else:                                                                             
      &lt;em&gt;Hello&lt;/em&gt;, world!                                                         
  &lt;br&gt;                                                                               
  $if datafile:                                                                      
      &lt;img src=$datafile alt="your picture"&gt;                                         
  &lt;br&gt;                                                                               
  &lt;a href="/hello"&gt;Go Back&lt;/a&gt;  
  </code></pre>
  
  <p>When I do this, I get a broken link for the image.  How do I get the image to display properly?  Ideally, I wouldn't have to read from disk to display it, although I'm not sure if that's possible.  Also, is there a way to write the file to the relative path, instead of the absolute path?</p>