Reputation: 1438
I'm using pyramid_handlers
for routing, controller.py
:
import pyramid_handlers
from blue_yellow_app.controllers.base_controller import BaseController
from blue_yellow_app.services.albums_service import AlbumsService
class AlbumsController(BaseController):
@pyramid_handlers.action(renderer='templates/albums/index.pt')
def index(self):
# data / service access
all_albums = AlbumsService.get_albums()
# return the model
return {'albums': all_albums}
And I've registered in __init__.py
like this:
from pyramid.config import Configurator
import blue_yellow_app.controllers.controller as albums
def main(_, **settings):
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.include('pyramid_handlers')
config.add_handler(
'albums' + 'ctrl_index', '/' + 'albums',
handler=albums.AlbumsController, action='index')
config.add_handler(
'albums' + 'ctrl_index/', '/' + 'albums' + '/',
handler=albums.AlbumsController, action='index')
config.add_handler(
'albums' + 'ctrl', '/' + 'albums' + '/{id}',
handler=albums.AlbumsController)
Now how can I add new controller view for one certain album? I've tried to add new view like this:
import pyramid_handlers
from blue_yellow_app.controllers.base_controller import BaseController
from blue_yellow_app.services.albums_service import AlbumsService
class AlbumsController(BaseController):
@pyramid_handlers.action(renderer='templates/albums/index.pt')
def index(self):
# data / service access
all_albums = AlbumsService.get_albums()
# return the model
return {'albums': all_albums}
@pyramid_handlers.action(
name='albums/{id}',
renderer='templates/albums/item.pt')
def album(self):
print ('test')
return {}
But it doesn't work. How to add view for rout albums/{id}
?
Upvotes: 1
Views: 100
Reputation: 3380
Looks like this code is from my Python for Entrepreneurs course. Let's focus on the add_handler section. The generic form of the function is:
config.add_handler(NAME, URL, handler=HANDLER, action=OPTIONAL_ACTION_METHOD)
You want to map the URL /albums/rock-classics
to the action method def album(self)
. In the add_handler call you have:
config.add_handler(
'albumsctrl', '/' + 'albums' + '/{id}',
handler=albums.AlbumsController)
The problem is two-fold:
You do not specify the action either in the routing values or in the function call. You should have:
# via add_handler, url: /albums/rock-classics
config.add_handler(
'albumsctrl', '/albums/{id}',
handler=albums.AlbumsController, action=album)
or
# via route, url: /albums/album/rock-classics
config.add_handler(
'albumsctrl', '/albums/{action}/{id}',
handler=albums.AlbumsController)
Second problem is your name in the action method itself
@pyramid_handlers.action(
name='albums/{id}', <----- PROBLEM: this is not a valid action name
renderer='templates/albums/item.pt')
def album(self):
print ('test')
return {}
It should either duplicate name='album' or just be the name of the method as:
@pyramid_handlers.action(renderer='templates/albums/item.pt')
def album(self):
print ('test')
return {}
Upvotes: 1