get(self) 需要 1 个参数,提供了 2 个 - 错误
我遇到了一个很奇怪的错误,具体情况如下:
class ServeHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, resource):
iconKey = str(urllib.unquote(resource))
if iconKey:
blob_info = blobstore.get(iconKey)
if blob_info:
url = images.get_serving_url(blob_key=iconKey, size=200)
self.response.out.write('<h1>%s</h1><small>%s</small><br/><br/><img src="%s" alt="%s">' % ('A Title', '11-26-1997', url, 'A Title'))
返回的结果是这个:
TypeError: get() takes exactly 1 argument (2 given)
这段代码本来是想从网址请求的最后部分提取信息,把它传给 iconKey
变量,然后用这个变量作为 blob 键去访问 blobstore,获取图片,并通过 images.get_serving_url()
方法生成一个可以使用的链接。
有没有人遇到过类似的问题?我试着在 get
方法的定义上加了一个 @staticmethod
参数,但这样一来,get
方法就无法通过 self
访问请求了。
编辑
我刚刚做了一些修改,结果又出现了一个错误。我之前使用的正则表达式是 ([^/]+)?
来处理网址,比如网址是 /view/icon/76M5e-xIStHRJDYyXBXjDA==
,而传给 get()
方法的资源就是网址最后的部分 76M5e-xIStHRJDYyXBXjDA==
。
我根据 @systempuntoout 的建议,把正则表达式改成了 (.*)
。现在我遇到了这个错误:AttributeError: split
,并且有这个错误堆栈信息:
ERROR 2011-07-15 13:19:39,949 __init__.py:463] split
Traceback (most recent call last):
File "/Users/mac/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 700, in __call__
handler.get(*groups)
File "/Users/mac/icondatabase/main.py", line 72, in get
iconKey = str(urllib.unquote(self.request))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/urllib.py", line 1164, in unquote
File "/Users/mac/Desktop/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webob/webob/__init__.py", line 500, in __getattr__
raise AttributeError(attr)
AttributeError: split
INFO 2011-07-15 13:19:39,958 dev_appserver.py:4217] "GET /view/icon/76M5e-xIStHRJDYyXBXjDA== HTTP/1.1" 500 -
INFO 2011-07-15 13:19:40,250 dev_appserver.py:4217] "GET /favicon.ico HTTP/1.1" 404 -
2 个回答
0
你把两个不同类的 get
方法搞混了:
BlobstoreDownloadHandler.get
需要两个参数,分别是self
和resource
。webapp.RequestHandler.get
只需要一个参数self
。
1
你可能没有在你的URL正则表达式配置中匹配到任何resource
组参数。
确保在你的主程序中有这样的规则:
application = webapp.WSGIApplication(
[(r'/files/(.*)', ServeHandler)], debug=True)
run_wsgi_app(application)
这样做会把在路由/files/
后面匹配到的字符串传递给ServeHandler实例的get()方法中的resource
参数。
举个例子:
localhost:8080/files/A2312ODESDX
会把A2312ODESDX
作为resource
传递。