Python Webapp2:将routes.PathPrefixRoute与扩展路由一起使用
我正在构建一个简单的RESTful API。当我对Webapp2的扩展路由使用Python Webapp2:将routes.PathPrefixRoute与扩展路由一起使用,python,google-app-engine,webapp2,Python,Google App Engine,Webapp2,我正在构建一个简单的RESTful API。当我对Webapp2的扩展路由使用routes.PathPrefixRoute时,它会抛出500个错误。使用不带PathPrefixRoute的常规扩展路由可以正常工作。是否有任何方法可以为路由添加前缀,但仍保留我的命名参数 import json import webapp2 from webapp2_extras import routes class GetAllTodos(webapp2.RequestHandler): def
routes.PathPrefixRoute
时,它会抛出500个错误。使用不带PathPrefixRoute
的常规扩展路由可以正常工作。是否有任何方法可以为路由添加前缀,但仍保留我的命名参数
import json
import webapp2
from webapp2_extras import routes
class GetAllTodos(webapp2.RequestHandler):
def get(self):
"""Returns a JSON formatted greeting"""
# TODO: Should retrieve data from a datastore
greeting = {'greeting': 'Hello there'}
json.dumps(greeting, sort_keys=True, indent=4)
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(json.dumps(greeting))
app = webapp2.WSGIApplication([
routes.PathPrefixRoute('/todos/api/v0.1.0', [
webapp2.Route(handler=GetAllTodos,
name='get-all-todos',
methods=['GET']),
]),
])
这是服务器的错误
ERROR 2014-10-22 23:56:14,980 wsgi.py:263]
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "/Users/ben.grunfeld/Desktop/Work/code/gae-restful-api/main.py", line 23, in <module>
methods=['GET']),
TypeError: __init__() takes at least 2 arguments (4 given)
INFO 2014-10-22 23:56:14,984 module.py:666] default: "GET /todos/api/v0.1.0 HTTP/1.1" 500 -
错误2014-10-22 23:56:14980 wsgi.py:263]
回溯(最近一次呼叫最后一次):
文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine default.bundle/Contents/Resources/google\u appengine/google/appengine/runtime/wsgi.py”,第240行,在句柄中
handler=\u config\u handle.add\u wsgi\u中间件(self.\u LoadHandler())
文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine default.bundle/Contents/Resources/google\u appengine/google/appengine/runtime/wsgi.py”,第299行,在\u LoadHandler中
处理程序,路径,err=LoadObject(self.\u处理程序)
LoadObject第85行的文件“/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine default.bundle/Contents/Resources/google_appengine/google/appengine/runtime/wsgi.py”
obj=\uuuuuuuuuuuuuu导入(路径[0])
文件“/Users/ben.grunfeld/Desktop/Work/code/gae restful api/main.py”,第23行,在
方法=['GET']),
TypeError:\uuuu init\uuuuu()至少接受2个参数(给定4个)
INFO 2014-10-22 23:56:14984 module.py:666]默认值:“GET/todos/api/v0.1.0 HTTP/1.1”500-
您的webapp2.Route
处理程序定义缺少将要处理的路由
试一试
我所做的只是添加了
'All'
部分,现在当您请求/todos/api/v0.1.0/All
时,您的GetAllTodos
的get
方法应该可以运行了。工作得很好。奖金问题-当我用
或
替换所有时,404退出。以/
作为前缀会引发500错误。有什么想法吗?@BenGrunfeld,请确保将处理程序定义为def get(self,todo_id)
,而不是def get(self)
。
app = webapp2.WSGIApplication([
routes.PathPrefixRoute('/todos/api/v0.1.0', [
webapp2.Route('all',
handler=GetAllTodos,
name='get-all-todos',
methods=['GET']),
]),
])