How to pass part of url id to view_callable?

I've been playing with Pyramid recently and, based on the Pylons background, I focused on URL routing, not on crawl.

I also consider using handlers to group specific controller functions into one class. Instead of 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 read the docs earlier today about the implicit declaration of an action in the add_handler () call, so that might be wrong ... However, my main problem is accessing the id in view_callable

How do I get the "id"?

+4
source share
1 answer

You can access the "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} 

Additional Information:

+9
source

Source: https://habr.com/ru/post/1334489/


All Articles