def paged template per_page def decorator func def wrapper request arg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def paged(template, per_page=5):
def decorator(func):
def wrapper(request, *args, **kwargs):
function = func(request, *args, **kwargs)
paginate_qs = function.pop('content')
pages = Paginator(paginate_qs, per_page)
page_num = function.get('page')
cur_page = pages.page(page_num)
if cur_page.has_next():
next_page = page_num + 2
if cur_page.has_previous():
next_page = page_num - 2
return render_to_response(template, {'all_content': cur_page.object_list })
return wrapper
return decorator
@paged('index/news_index.html')
def index(request):
all_content = MainMessage.objects.all().order_by('-date_mes')[:5]
try:
page = int(request.GET.get('page', 1))
except ValueError:
page = 1
return {'content': all_content, 'page': page}