2017-01-28 40 views
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}

+0

'pyramid_handlers' package has n已经更新了很长一段时间。也许如果你在金字塔中将问题编辑为“我怎样才能达到X”,我们可以给你一个普通的回应,而不需要任何外部软件包。 –

+0

我们可能也想引入Michael Kennedy,因为我从他的课件中识别出这个源代码:https://github.com/mikeckennedy/python-for-entrepreneurs-course-demos/search?utf8 =%E2%9C% 93&q = pyramid_handlers&type =代码 –

+0

Mikko:处理程序尚未更新一段时间。但我认为这更像是一种“完成”的情况,而不是错误的方式。最后一次URL调度发生了什么变化?我实际上并不知道,但我怀疑它在几年内也没什么大不了的。 –

回答

1

貌似这个代码是从我Python for Entrepreneurs course。我们来关注add_handler部分。该函数的一般形式是:

config.add_handler(NAME, URL, handler=HANDLER, action=OPTIONAL_ACTION_METHOD) 

你想要的网址/albums/rock-classics映射到操作方法def album(self)。在add_handler叫你:

config.add_handler(
    'albumsctrl', '/' + 'albums' + '/{id}', 
    handler=albums.AlbumsController) 

的问题是双重的:

你没有指定路由值或在函数调用的动作。你应该有:

# via add_handler, url: /albums/rock-classics 
config.add_handler(
    'albumsctrl', '/albums/{id}', 
    handler=albums.AlbumsController, action=album) 

# via route, url: /albums/album/rock-classics 
config.add_handler(
    'albumsctrl', '/albums/{action}/{id}', 
    handler=albums.AlbumsController) 

第二个问题是在操作方法本身

@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 {} 

应该要么重复名称=“专辑”或仅仅是名称你的名字的方法为:

@pyramid_handlers.action(renderer='templates/albums/item.pt') 
def album(self): 
    print ('test') 
    return {}