Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass in the 'id' portion of the URL to a view_callable?

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

like image 210
dave Avatar asked Jan 06 '11 11:01

dave


1 Answers

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:

  • URL Dispatch: The matchdict
  • Defending Pyramid's Design: Pyramid views do not accept arbitrary keyword arguments
like image 94
Blaise Laflamme Avatar answered Nov 14 '22 23:11

Blaise Laflamme