404 找不到资源 [英] 404 The resource could not be found

查看:46
本文介绍了404 找不到资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试以匿名身份访问单个项目时,未找到获取 404 资源,即未通过身份验证.我希望改为 403 Forbidden,因为我对视图配置有设置权限.

Getting 404 Resource not found when trying to access a single item as anonymous i.e. no authenticated. I would expect instead a 403 Forbidden since I have setting permission on the view config.

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()

我的视图初始化的遍历设置为:

My views init has the traversal set as:

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

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

Log shows that the route was matched though then shows 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: ()

当用户登录时,视图呈现良好,如果没有,我想改为获取禁止视图.

The view render fine when the user is logged in just that when not, I would like to get Forbidden view instead.

推荐答案

看起来你的上下文是 BookContainerview_name8LjkOSzGSR67i1dnGUOg-Q代码>.这与您所需的 BookResource 上下文与 ''name 不匹配,因此在检查权限之前将是 404.您可能希望从 view_config 中删除 name 谓词,因为您可能并不关心名称是什么.还要根据遍历树中发生的情况找出上下文不是您期望的原因.

It looks like your context is BookContainer with a view_name of 8LjkOSzGSR67i1dnGUOg-Q. This does not match your required context of BookResource with a name of '' and thus would be a 404 before permissions are checked. You probably want to drop the name predicate from your view_config because you probably do not care what the name is. Also figure out why the context is not what you expect based on what's happening in your traversal tree.

这篇关于404 找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆