Home > pylons, python > Pylons: Paginate parte 2

Pylons: Paginate parte 2

20 Ottobre 2011

Paginate: AttributeError: ‘thread._local’ object has no attribute ‘mapper’

Nell’articolo precedente abbiamo sistemato il paginate.pager, ora
sistemiamo anche l’action del controller Page e la template, per allinearci
al capitolo 8 (SimpleSite) del Pylons Book.

...
from routes import Mapper
...

class PageController(BaseController):
    ...
    def list(self, id = None):
        mapper = Mapper()
        mapper.connect(':page')
        records = meta.Session.query(model.Page).all()
        if id is None:
            c.num = 1
        else:
            c.num = id
        c.paginator = paginate.Page(records, page = int(request.params.get('page', c.num)),
                                    items_per_page = 1,)
        return render('/derived/page/list.html')

il template list.html

<%inherit file="/base/index.html" />

<%def name="heading()"><h1>Page List</h1></%def>

<%def name="buildrow(page, odd=True)">
	%if odd:
		<tr class="odd">
	%else:
		<tr class="even">
	% endif

	<td valign="top">
		${h.link_to(page.id,
				h.url(controller=u'page',
					action='view',
					id=unicode(page.id)))}
	</td>

	<td valign="top">
		${page.title}
	</td>
	<td valign="top">${page.posted.strftime('%c')}</td>

	</tr>
</%def>

% if len(c.paginator):
	<p>Page : ${c.paginator.pager('$link_first $link_previous $first_item to $last_item of $item_count $link_next $link_last') }</p>

	<table class="paginator"><tr><th>Page ID</th><th>Page Title</th><th>Posted</th></tr>
	<% counter=0 %>

	% for item in c.paginator:
		${buildrow(item, counter%2)}
		<% counter += 1 %>
	% endfor
	</table>
	<p>${ c.paginator.pager('~2~') }</p>
% else:
	<p>
	No pages have yet been created.
	<a href="${h.url(controller='page', action='new')}">Add one</a>.
	</p>
% endif

Visitando http://localhost:5000/page/list, funziona tutto.

Categorie:pylons, python Tag:
I commenti sono chiusi.