0

我正在尝试在 GAE 上制作一个简单的应用程序,允许用户输入图像的 url 和名称。然后,该应用程序将此图像连同其名称一起上传到数据存储区。

上传后页面自我重定向,然后应该将图像发送回客户端并显示在他们的机器上。

运行后我得到的是服务器错误。由于我是 GAE 新手,请有人告诉我我的代码是否至少正确。

我看不出我的代码有什么问题。(我已经检查了正确的缩进和空格)。下面是代码:

蟒蛇:

import jinja2   # html template libary
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

import urllib   
import urllib2 
import webapp2

from google.appengine.ext import db
from google.appengine.api import urlfetch


class Default_tiles(db.Model):
  name = db.StringProperty()
  image = db.BlobProperty(default=None)


class MainPage(webapp2.RequestHandler):
  def get(self):

    template = jinja_environment.get_template('index.html')
    self.response.out.write(template.render())

class Upload(webapp2.RequestHandler):
  def post(self):

    # get information from form post upload
    image_url = self.request.get('image_url') 
    image_name = self.request.get('image_name')

    # create database entry for uploaded image 
    default_tile = Default_tiles()
    default_tile.name = image_name
    default_tile.image = db.Blob(urlfetch.Fetch(image_url).content)
    default_tile.put()

    self.redirect('/?' + urllib.urlencode({'image_name': image_name}))


class Get_default_tile(webapp.RequestHandler):
  def get(self):
    name = self.request.get('image_name')
    default_tile = get_default_tile(name)

    self.response.headers['Content-Type'] = "image/png"
    self.response.out.write(default_tile.image)

    def get_default_tile(name):
      result = db.GqlQuery("SELECT * FROM Default_tiles WHERE name = :1 LIMIT 1", name).fetch(1)
      if (len(result) > 0):
        return result[0]
      else:
        return None


app = webapp2.WSGIApplication([('/', MainPage),
                               ('/upload', Upload),
                               ('/default_tile_img', Get_default_tile)],
                              debug=True)

的HTML:

<html>
    <head>
        <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
    </head>

    <body>
        <form action="/upload" method="post">
            <div>
                <p>Name: </p>
                <input name="image_name">
            </div>
            <div>
                <p>URL: </p>
                <input name="image_url">
            </div>
            <div><input type="submit" value="Upload Image"></div>
        </form>
        <img src="default_tile_img?{{ image_name }}">

    </body>

</html>

任何帮助都将非常感激。谢谢!

更新

感谢 Greg,我知道如何查看错误日志。正如格雷格所说,我错过了一个逗号,我已经更新了上面的代码。

该应用程序现在运行,但是当我上传图像时,返回时没有显示图像。我在日志中收到以下消息:

文件“/Users/jamiefearon/Desktop/Development/My Programs/GAE 具有 css、javascript 和图像的全功能网站/mywebsite.py”,第 53 行,在 get default_tile = self.get_default_tile(name) TypeError: get_default_tile() 中1 个参数(给定 2 个)

我只向get_default_tile()传递了一个参数,为什么它抱怨我传递了两个?

4

2 回答 2

2

您在('/upload', Upload)WSGIApplication 设置之后缺少一个逗号。

于 2013-02-02T01:00:22.177 回答
0

使用这个 python 代码

import jinja2   # html template libary
import os

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

import urllib   
import urllib2 
import webapp2

from google.appengine.ext import db
from google.appengine.api import urlfetch


class Default_tiles(db.Model):
    name = db.StringProperty()
    image = db.BlobProperty(default=None)


class MainPage(webapp2.RequestHandler):
    def get(self):
      template = jinja_environment.get_template('index.html')
      self.response.out.write(template.render())

class Upload(webapp2.RequestHandler):
    def post(self):
      # get information from form post upload
      image_url = self.request.get('image_url') 
      image_name = self.request.get('image_name')
      # create database entry for uploaded image 
      default_tile = Default_tiles()
      default_tile.name = image_name
      default_tile.image = db.Blob(urlfetch.Fetch(image_url).content)
      default_tile.put()
      self.redirect('/?' + urllib.urlencode({'image_name': image_name}))


class Get_default_tile(webapp2.RequestHandler):
    def get_default_tile(self, name):
        result = db.GqlQuery("SELECT * FROM Default_tiles WHERE name = :1 LIMIT 1", name).fetch(1)
        if (len(result) > 0):
          return result[0]
        else:
          return None
    def get(self):
      name = self.request.get('image_name')
      default_tile = self.get_default_tile(name)
      self.response.headers['Content-Type'] = "image/png"
      self.response.out.write(default_tile)




app = webapp2.WSGIApplication([('/', MainPage),
                               ('/upload', Upload),
                               ('/default_tile_img', Get_default_tile)],
                              debug=True)
于 2013-10-10T13:03:08.767 回答