class RespondersAPI(MethodView):
# some class based view to do restful services
...
#now we have url maps here
users_api = RespondersAPI.as_view('users_api', userlevel=1)
admin_mod.add_url_rule("/user/uid/<objectid:user_id>",
view_func = users_api,
methods=["PUT", "DELETE"])
admin_mod.add_url_rule("/user",
defaults={'page': 1},
view_func = users_api,
methods=["GET"])
admin_mod.add_url_rule("/user/page/<int:page>",
view_func = users_api,
methods=["GET"])
admin_mod.add_url_rule("/user/new",
view_func = users_api,
methods=["POST"],)
从上面可以看出,问题是无法达到第4个路由规则。 objectid转换器就是这里发生的事情:
>>> url_for('.users_api', page=1)
'/admin/user'
>>> url_for('.users_api', page=2)
'/admin/user/page/2'
>>> url_for('.users_api', user_id=users[0].id)
'/admin/user?user_id=521781f4fe8974125cef5ea9' # not what I expected
objectid转换器是the one from Armin Ronacher。我在那里放了一些调试代码,发现这甚至没有被解雇。我认为这与创建url规则的顺序有关,所以我尝试在声明所有其他规则之前用objectid声明url规则;但这根本没有用。 我的代码出了什么问题?这是当前的网址:
Map([
<Rule '/admin/user/new' (POST, OPTIONS) -> admin_mod.users_api>,
<Rule '/admin/user/uid/<user_id>' (PUT, OPTIONS, DELETE) -> admin_mod.users_api>,
<Rule '/admin/user/page/<page>' (HEAD, OPTIONS, GET) -> admin_mod.users_api>,
<Rule '/admin/user' (HEAD, OPTIONS, GET) -> admin_mod.users_api>,
... # and a bunch of rules ..
])