views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from blog.data.models import Entry
def index(request):
entries = Entry.objects.published_entries().order_by('-id')
paginator = Paginator(entries, 2)
page_num = request.GET.get('page', 1)
try:
page = paginator.page(page_num)
except EmptyPage:
page = paginator.page(paginator.num_pages)
except PageNotAnInteger:
page = paginator.page(1)
return render_to_response('homepage/index.html', {'page': page}, context_instance = RequestContext(request))
--------------------------------------------------------------------------------------------
index.html
{% extends "homepage/base.html" %}
{% block content %}
<h3>Entries</h3>
{% for entry in page.object_list %}
<div>{{ entry.title }} - {{ entry.created }}</div>
<div>{{ entry.text }}</div>
<br />
{% endfor %}
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">prev</a>
{% endif %}
| {{ page.number }} |
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">next</a>
{% endif %}
{% endblock %}
Комментариев нет:
Отправить комментарий