尝试从GAE动态提供图像,获得404

时间:2012-12-18 17:31:36

标签: python google-app-engine

我的图片没有从数据存储区提供,我在图片应该是的页面上得到404.

以下是代码:

import cgi
import datetime
import urllib
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.api import images
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app


class PropUpload(db.Model):
  property_name = db.StringProperty()
  property_address = db.StringProperty()
  project_type = db.StringProperty()
  building_size = db.StringProperty()
  stories = db.StringProperty()
  space_available = db.StringProperty()
  per_leased = db.StringProperty()
  prop_image = db.BlobProperty()
  date = db.DateTimeProperty(auto_now_add=True)


class MainPage(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""<!DOCTYPE html>
            <html lang="en">
              <head>
      <somehtml>""")

    display2 = db.Query(PropUpload)
    display2.order("-date")
    for dis in display2:
        self.response.out.write('<div><img src="img?img_id=%s"></img>' % dis.key())
        self.response.out.write('<h4>Property Name: %s </h4><br>' % dis.property_name)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Address: %s </div><br>' % dis.property_address)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Type:  %s ft<sup>2</sup></div><br>' % dis.project_type)
        self.response.out.write('<div style="border-bottom: 1px solid #CCC;">Stories:  %s </div><br>' % dis.stories)

    self.response.out.write("<more html>")


class Image(webapp.RequestHandler):
  def get(self):
    img_id = self.request.get("img_id")
    dis = db.get(self.request.get("img_id"))
    if dis.prop_image:
      self.response.headers['Content-Type'] = "image/png"
      self.response.out.write(dis.prop_image)
    else:
      self.response.out.write("no image")


class UploadPage2(webapp.RequestHandler):
  def get(self):
    self.response.out.write("""<!DOCTYPE html>
    <form action="/upload2" enctype="multipart/form-data" method="post">
                        <div><label>Property Name:&nbsp</label></div>
                        <div><textarea name="prop-name" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Property Address:&nbsp</label></div>
                        <div><textarea name="address" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Property Type:&nbsp</label></div>
                        <div><textarea name="prop-type" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Building Size:&nbsp</label></div>
                        <div><textarea name="size" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Stories:&nbsp</label></div>
                        <div><textarea name="stories" rows="1" columns "100"></textarea></div>
                        <br><br>
                        <div><label>Space Available:&nbsp</label></div>
                        <div><textarea name="title" rows="2" columns "100"></textarea></div>
                        <br>
                        <div><label>Percent Leased:</label></div><span>
                        <div><textarea name="percent" rows="3" cols="100"></textarea></div><br>
                        <div><label>Picture:</label></div>
                        <div><input type="file" name="img"/></div>
                        <div><label></label></div><span>
                        <div><input type="submit" value="Upload" /></div>
                      </form>""")


  def post(self):
    props = PropUpload()
    property_name = self.request.get('prop-name')
    property_address = self.request.get('address')
    property_type = self.request.get('prop=type')
    building_size = self.request.get('size')
    stories = self.request.get('stories')
    space_available = self.request.get('space')
    prop_image = self.request.get('img')
    per_leased = self.request.get('percent')
    props.property_name = property_name
    props.property_address = property_address
    props.property_type = property_type
    props.building_size = building_size
    props.stories = stories
    props.space_available = space_available
    props.prop_image = db.Blob(prop_image)
    props.per_leased = per_leased
    props.put()
    self.redirect('/upload2')


application = webapp.WSGIApplication([
    ('/', MainPage),
    ('/upload2', UploadPage2),
    ], debug=True)


def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

一切都运行良好,除了404,我找不到任何搜索问题。

感谢您的帮助,对不起,如果我遗漏了任何内容,我会编辑任何内容。

1 个答案:

答案 0 :(得分:2)

假设您的上传工作正常,您需要为图片本身提供处理程序。目前您正在引用<img src="img?img_id=%s">,但img似乎无法解析处理程序中的任何内容(同样,您无需向{{{{}}提供结束</img>标记1}} HTML中的属性)。您要做的是为基于图像的请求设置处理程序,并将响应作为图像本身。您已经设置<img>用于显示该目的的内容(请注意,您可能希望将Image部分替换为实际图像,以便结果类型保持一致)。

为了将no image处理程序映射到URL,请尝试按如下方式修改处理程序:

Image

现在,这会将application = webapp.WSGIApplication([ ('/', MainPage), ('/img', Image), ('/upload2', UploadPage2), ], debug=True) 的请求转发给/img处理程序(您可能还希望将Image标记更改为src,然后找到并返回你的形象。