Add pagination support for blog listing

This commit is contained in:
Gergely Polonkai 2013-10-14 12:12:50 +02:00
parent 7b3ec3f916
commit 87d7e16b0e
4 changed files with 58 additions and 11 deletions

View File

@ -2,11 +2,13 @@
{% block content %}
{% if posts %}
{% include 'paginator.html' with list=posts %}
{% for post in posts %}
{% with title_links=True %}
{% include 'blog/post.html' %}
{% endwith %}
{% endfor %}
{% include 'paginator.html' with list=posts %}
{% else %}
<p>No posts are returned for your query.</p>
{% endif %}

View File

@ -2,8 +2,10 @@ from django.conf.urls import patterns, url
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^feed$', views.feed, name='feed'),
url(r'^tag/(?P<tag>.*)$', views.taglist, name='taglist'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
url(r'^$', views.index, name='index'),
url(r'^page/(?P<page>\d+)$', views.indexpage, name='indexpage'),
url(r'^tag/(?P<tag>[^/]+)$', views.taglist, name='taglist'),
url(r'^tag/(?P<tag>[^/]+)/page/(?P<page>\d+)$', views.tagpage, name='tagpage'),
url(r'^(?P<year>\d+)/(?P<month>\d+)/(?P<day>\d+)/(?P<slug>[^/]+)$', views.read, name='read'),
url(r'^feed$', views.feed, name='feed'),
)

View File

@ -1,23 +1,47 @@
import datetime
from django.shortcuts import render, get_object_or_404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from blog.models import Post
def mainpage(request):
last_posts = Post.objects.filter(draft=False).order_by('-created_at')[:5]
last_posts = Post.objects.filter(draft = False).order_by('-created_at')[:5]
return render(request, 'blog/listing.html', {'posts': last_posts})
def listing(request, tag, page):
if (tag == None):
post_list = Post.objects.filter(draft = False)
view = "index"
else:
post_list = Post.objects.filter(tags__slug = tag, draft = False)
view = "tagage"
paginator = Paginator(post_list.order_by('-created_at'), 10)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request, 'blog/listing.html', { 'posts': posts, 'view': "blog:" + view })
def index(request):
last_posts = Post.objects.filter(draft=False).order_by('-created_at')
return render(request, 'blog/listing.html', {'posts': last_posts})
return listing(request, None, 1)
def indexpage(request, page):
return listing(request, None, page)
def taglist(request, tag):
return listing(request, tag, 1)
def tagpage(request, tag, page):
return listing(request, tag, page)
def read(request, year, month, day, slug):
post = get_object_or_404(Post, created_at__year=int(year), created_at__month=int(month), created_at__day=int(day), slug=slug, draft=False);
return render(request, 'blog/view.html', {'post': post})
def taglist(request, tag):
posts = Post.objects.filter(tags__slug=tag, draft=False)
return render(request, 'blog/listing.html', {'posts': posts})
def feed(request):
return render(request, 'blog/feed.xml', {})

View File

@ -0,0 +1,19 @@
{% if list.paginator.num_pages > 1 %}
<div class="paginator">
{% if list.number > 1 %}
<a href="{% url view %}">First</a>
{% endif %}
{% if list.has_previous and list.number != 2 %}
<a href="{% url view|add:"page" list.previous_page_number %}">Previous</a>
{% endif %}
{% for i in posts.paginator.page_range %}
{% if list.number != i %}<a href="{% url view|add:"page" i %}">{% endif %}{{ i }}{% if list.number != i %}</a>{% endif %}
{% endfor %}
{% if list.has_next and list.number != list.paginator.num_pages|add:-1 %}
<a href="{% url view|add:"page" list.next_page_number %}">Next</a>
{% endif %}
{% if list.number < list.paginator.num_pages %}
<a href="{% url view|add:"page" list.paginator.num_pages %}">Last</a>
{% endif %}
</div>
{% endif %}