1

尝试以匿名方式访问单个项目(即未经过身份验证)时找不到 404 资源。我希望改为 403 Forbidden 因为我在视图配置上设置了权限。

class BookShow(FormView):
"""Show one instance of a model."""

    resource_buttons = [...]

    @view_config(route_name="book", context=BookResource, name="", renderer="site/workspace/book/single.html", permission="authenticated")
    def book(self):
        ...
        return locals()

我的视图 init 将遍历设置为:

self.config.add_route('book', '/book/*traverse', factory="bookstoreapp.views.bok.views.book_container_factory")

日志显示路由匹配,但随后显示 404 debug_notfound:

2017-04-08 12:36:09 mamachine pyramid_debug[685] DEBUG route matched for url http://localhost:6543/book/8LjkOSzGSR67i1dnGUOg-Q; route_name: 'book', path_info: '/book/8LjkOSzGSR67i1dnGUOg-Q', pattern: '/book/*traverse', matchdict: {'traverse': ('8LjkOSzGSR67i1dnGUOg-Q',)}, predicates: ''
2017-04-08 12:36:09 mamachine pyramid_debug[685] DEBUG debug_notfound of url http://localhost:6543/book/8LjkOSzGSR67i1dnGUOg-Q; path_info: '/book/8LjkOSzGSR67i1dnGUOg-Q', context: <bookstoreapp.views.book.views.BookContainer object at 0x7eff6d5d1fd0>, view_name: '8LjkOSzGSR67i1dnGUOg-Q', subpath: (), traversed: (), root: <bookstoreapp.views.book.views.BookContainer object at 0x7eff6d5d1fd0>, vroot: <bookstoreapp.views.book.views.BookContainer object at 0x7eff6d5d1fd0>, vroot_path: ()

当用户登录时,视图呈现良好,只是在没有登录时,我想获得 Forbidden 视图。

4

1 回答 1

1

看起来您的上下文BookContainer与 a view_nameof 8LjkOSzGSR67i1dnGUOg-Q. BookResource这与您所需的with a nameof上下文不匹配'',因此在检查权限之前将是 404。您可能想name从您的谓词中删除该谓词,view_config因为您可能不关心名称是什么。还要根据遍历树中发生的情况找出为什么上下文不是您所期望的。

于 2017-04-08T22:08:56.383 回答