提供一个控制器和方法,如:
import genshi
from pylons import tmpl_context, request, url
import re
from tg import expose, flash
from tg.controllers import redirect
from tg.decorators import validate
import tw.forms as twf
from tw.forms.datagrid import DataGrid
from tg.decorators import paginate
from tg.i18n import ugettext as _
from repoze.what.predicates import has_permission
from interlock.lib.base import BaseController
assigned_seller_product_grid = DataGrid(fields=[
('Product Name', 'name'),
('Producer', 'producer'),
])
unassigned_seller_product_grid = DataGrid(fields=[
('Product Name', 'name'),
('Producer', 'producer'),
])
class SellerController(BaseController):
allow_only = has_permission('manage')
@paginate("unassigned", items_per_page=5, use_prefix=True)
@paginate("assigned", items_per_page=5, use_prefix=True)
@expose('demo.templates.sellers.products')
def products(self, uid = None, **kw):
seller = uid is not None and DBSession.query(SellerModel).filter_by(uid=uid, deleted=False).first()
if uid is not None and not seller:
flash(_('No Seller ID Defined'), 'warning')
redirect('/seller/')
unassigned = DBSession.query(ProductModel).filter(~ProductModel.sellers.any(SellerModel.uid == seller.uid)).filter(ProductModel.deleted == False)
assigned = DBSession.query(ProductModel).filter(ProductModel.sellers.any(SellerModel.uid == seller.uid)).filter(ProductModel.deleted == False)
return dict(seller=seller,
assigned=assigned,
unassigned = unassigned,
assigned_grid=assigned_seller_product_grid,
unassigned_grid=unassigned_seller_product_grid,
page='seller')
还有一个模板:
<h1>Assigned Products</h1>
<div py:if="assigned">${assigned_grid(assigned)}</div>
<div>${tmpl_context.paginators.assigned.pager(page_param='assigned_page')}</div>
<h1>Unassigned Products</h1>
<div py:if="unassigned">${unassigned_grid(unassigned)}</div>
<div>${tmpl_context.paginators.unassigned.pager(page_param='unassigned_page')}</div>
我看到TG2正确生成了分页链接,但是分页只是简单地使用第一个装饰器更改页面,忽略第二个装饰器。有没有人以前遇到过这个问题?
感谢。
答案 0 :(得分:1)
TG上当前的 @paginate 装饰器不允许在同一页面上处理多个分页器。下一版本将提供新版本的装饰器。与此同时,您可以使用以下实现替换 @paginate 装饰器(只需将其另存为myproject.lib.paginate并导入而不是tg.decorators.paginate):
from tg.decorators import Decoration
from tg.paginate import Page
from tg.util import Bunch, partial
from tg import request
class paginate(object):
def __init__(self, name, use_prefix=False,
items_per_page=10, max_items_per_page=0):
self.name = name
prefix = use_prefix and name + '_' or ''
self.page_param = prefix + 'page'
self.items_per_page_param = prefix + 'items_per_page'
self.items_per_page = items_per_page
self.max_items_per_page = max_items_per_page
def __call__(self, func):
decoration = Decoration.get_decoration(func)
decoration.register_hook('before_validate', self.before_validate)
decoration.register_hook('before_render', self.before_render)
return func
def before_validate(self, remainder, params):
page_param = params.pop(self.page_param, None)
if page_param:
try:
page = int(page_param)
if page < 1:
raise ValueError
except ValueError:
page = 1
else:
page = 1
try:
paginators_data = request.paginators
except:
paginators_data = request.paginators = {'_tg_paginators_params':{}}
paginators_data['_tg_paginators_params'][self.page_param] = page_param
paginators_data[self.name] = paginator = Bunch()
paginator.paginate_page = page or 1
items_per_page = params.pop(self.items_per_page_param, None)
if items_per_page:
try:
items_per_page = min(
int(items_per_page), self.max_items_per_page)
if items_per_page < 1:
raise ValueError
except ValueError:
items_per_page = self.items_per_page
else:
items_per_page = self.items_per_page
paginator.paginate_items_per_page = items_per_page
paginator.paginate_params = params.copy()
paginator.paginate_params.update(paginators_data['_tg_paginators_params'])
if items_per_page != self.items_per_page:
paginator.paginate_params[self.items_per_page_param] = items_per_page
def before_render(self, remainder, params, output):
if not isinstance(output, dict) or not self.name in output:
return
paginator = request.paginators[self.name]
collection = output[self.name]
page = Page(collection, paginator.paginate_page,
paginator.paginate_items_per_page, controller='/')
page.kwargs = paginator.paginate_params
if self.page_param != 'name':
page.pager = partial(page.pager, page_param=self.page_param)
if not getattr(tmpl_context, 'paginators', None):
tmpl_context.paginators = Bunch()
tmpl_context.paginators[self.name] = output[self.name] = page
答案 1 :(得分:1)
使用use_prefix attr并将其值设置为True
例如:
@paginate("movie1",items_per_page=5, use_prefix=True)
@paginate("movie2",items_per_page=5, use_prefix=True)
适用于Turbo-gears验证