1
我使用pyramid_handlers
路由,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}
而且我已经注册__init__.py
这样的:
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)
现在,我怎么加新的控制器视图为某个相册?我试过 增加新的观点是这样的:
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 {}
但它不工作。如何添加查看路线albums/{id}
?
'pyramid_handlers' package has n已经更新了很长一段时间。也许如果你在金字塔中将问题编辑为“我怎样才能达到X”,我们可以给你一个普通的回应,而不需要任何外部软件包。 –
我们可能也想引入Michael Kennedy,因为我从他的课件中识别出这个源代码:https://github.com/mikeckennedy/python-for-entrepreneurs-course-demos/search?utf8 =%E2%9C% 93&q = pyramid_handlers&type =代码 –
Mikko:处理程序尚未更新一段时间。但我认为这更像是一种“完成”的情况,而不是错误的方式。最后一次URL调度发生了什么变化?我实际上并不知道,但我怀疑它在几年内也没什么大不了的。 –