Question

I've been playing around with Pyramid lately and, coming from a Pylons background, I've been focusing in URL routing rather than traversal.

I've also been looking at using handlers to group together 'controller' specific functions into the one class. Rather than having view.py polluted with a bunch of functions.

Config:

config.add_handler('view_page', '/page/view/{id}', handler=Page, action=view_page)

Handler:

from pyramid.response import Response
from pyramid.view import action

class Page(object):

    def __init__(self, request):
        self.request = request

    def view_page(self):
        return {'id': id}

I was reading the docs earlier today regarding the implicit declaration of the action in the add_handler() call so that may be wrong... Nevertheless, my main problem is with accessing the id within the view_callable

How do I get 'id'?

Was it helpful?

Solution

You can access «id» through request.matchdict:

from pyramid.response import Response
from pyramid.view import action

class Page(object):

    def __init__(self, request):
        self.request = request

    def view_page(self):
        matchdict = request.matchdict
        id = matchdict.get('id', None)
        return {'id': id}

More info:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top