1

在 GAE 中,我收到一条错误消息,告诉我:

TypeError: get_default_tile() 只需要 1 个参数(给定 2 个)

如您所见,我的主 py 文件get_default_tile()中的代码仅传递了一个参数,即name

 default_tile = self.get_default_tile(name)

完整代码如下:

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):

    # this just prints out the url which the user enters into input
    image_name = self.request.get('image_name') 

    template_values = {
        'image_name': image_name,
    }

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



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(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.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)

任何帮助,将不胜感激。

4

2 回答 2

6

get_default_tile():是类的成员方法,Get_default_tile因此您需要像这样定义它:

def get_default_tile(self, name):

或者,如果您希望它是静态方法:

@staticmethod
def get_default_tile(name):
于 2013-02-02T01:46:05.190 回答
0

好的,我也犯了这个错误,但代码如下:

类 myclassname(webapp2.RequestHandler):

 def get(self):

     self.response.write('hola')

我用=解决了

类 myclassname(webapp2.RequestHandler):

 def get(self, **name**):

     self.response.write('hola')
于 2014-05-01T04:12:55.933 回答