0
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 转换器是来自 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 ..
])
4

0 回答 0