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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With