Search code examples
pythonaclpyramid

View is not found after adding a context


I'm trying to add an ACL to a view in a Pyramid project

I've got a working minimal test:

config.add_route('foo', '/foo')

view:

@view_config(route_name='foo', renderer='../templates/foo.jinja2',
             permission='view', context=Foo)
def foo(request):
    return {}

context:

class Foo:
    __acl__ = [
        (Allow, Everyone, 'view'),
    ]

Which works great without the permission and context set, but after adding them, I get pyramid.httpexceptions.HTTPNotFound: /foo.

Why is this route suddenly not found? This is almost exactly copied from the tutorial.


Solution

  • You should not specify the context in the view_config, but specify a factory in your route.

    config.add_route('foo', '/foo', factory=lambda r: Foo(r))
    

    Also, you may have to accept the request in your __init__:

    class Foo:
        def __init__(self, request):
            pass
    
        __acl__ = [
            (Allow, Everyone, 'view'),
        ]
    

    The way you did it (using context in the view_config) means "if the context is Foo, use that view function". It's a predicate.