문제

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'?

도움이 되었습니까?

해결책

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:

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top