pythonpyramidkotti

Can you have multiple generic subviews of a content element in Kotti?


You can add a 'view' for a content type in kotti by doing something along these lines:

from kotti_mysite.views import poll_view

config.add_view(
    poll_view,
    context=Poll,
    name='view',
    permission='view',
    renderer='kotti_mysite:templates/poll.pt',
)

(more details: http://kotti.readthedocs.org/en/latest/first_steps/tut-2.html)

You can also have multiple views, and use the 'set default view', but sometimes it's convenient to have several similar views with very similar urls.

For example, in plone, its trivial to have a url structure like this:

You can... sort of, do a similar thing in kotti by screwing with the view you create and rendering different content based on get/post params, but it's messy, and frankly, rather rubbish.

The only solution I've found is to have a custom content type 'JsonView' that has a json renderer, and add it as a child of the parent object, and it's renderer looks for the parent content, and renders that.

However, doing this requires you to manually create a 'JsonView' child for every instance of the type you want, which is also rather cumbersome.

Is there a better way of doing this?

--

Nb. Specifically note that having a custom view /blah/item/json isn't any use at all; any type of item, in any parent folder should be able to render in the way described above; using a single static route isn't the right solution.


Solution

  • You can register a json view for all your content like this:

    from kotti.interfaces import IContent
    
    config.add_view(
        my_json_view,
        context=IContent,
        name='json',
        permission='view',
        renderer='json',
    )
    

    This way, when you open /blah/json, where /blah points to some content, it will call your my_json_view view.

    SQLAlchemy's new class object inspection system might help you write a useful generic json view that works for more than one content type. Alternatively, you can register your view for specific content types only (by use of a more specific context argument in config.add_view).

    Using renderer='json' you tell Pyramid that you want to use its json renderer.