diff --git a/blog/templates/blog/listing.html b/blog/templates/blog/listing.html index c1587cc..9c9427f 100644 --- a/blog/templates/blog/listing.html +++ b/blog/templates/blog/listing.html @@ -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 %}

No posts are returned for your query.

{% endif %} diff --git a/blog/urls.py b/blog/urls.py index b11cbac..e7d2d96 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -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.*)$', views.taglist, name='taglist'), - url(r'^(?P\d+)/(?P\d+)/(?P\d+)/(?P[^/]+)$', views.read, name='read'), + url(r'^$', views.index, name='index'), + url(r'^page/(?P\d+)$', views.indexpage, name='indexpage'), + url(r'^tag/(?P[^/]+)$', views.taglist, name='taglist'), + url(r'^tag/(?P[^/]+)/page/(?P\d+)$', views.tagpage, name='tagpage'), + url(r'^(?P\d+)/(?P\d+)/(?P\d+)/(?P[^/]+)$', views.read, name='read'), + url(r'^feed$', views.feed, name='feed'), ) diff --git a/blog/views.py b/blog/views.py index 5d0b1d5..e57401d 100644 --- a/blog/views.py +++ b/blog/views.py @@ -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', {}) diff --git a/gergelypolonkaiweb/templates/paginator.html b/gergelypolonkaiweb/templates/paginator.html new file mode 100644 index 0000000..16734d2 --- /dev/null +++ b/gergelypolonkaiweb/templates/paginator.html @@ -0,0 +1,19 @@ +{% if list.paginator.num_pages > 1 %} +
+ {% if list.number > 1 %} + First + {% endif %} + {% if list.has_previous and list.number != 2 %} + Previous + {% endif %} + {% for i in posts.paginator.page_range %} + {% if list.number != i %}{% endif %}{{ i }}{% if list.number != i %}{% endif %} + {% endfor %} + {% if list.has_next and list.number != list.paginator.num_pages|add:-1 %} + Next + {% endif %} + {% if list.number < list.paginator.num_pages %} + Last + {% endif %} +
+{% endif %}