From a5445a03e08a5e7b7177bdeefbe82fc20547a654 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 19 Jun 2014 14:25:23 +0000 Subject: [PATCH] Add previous/next article links to blog articles --- blog/templates/blog/post.html | 6 ++++++ blog/views.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/blog/templates/blog/post.html b/blog/templates/blog/post.html index 5e3b4a0..087b195 100644 --- a/blog/templates/blog/post.html +++ b/blog/templates/blog/post.html @@ -29,4 +29,10 @@ comments powered by Disqus +{% if prev_post %} +Previous: {{ prev_post.title }} +{% endif %} +{% if next_post %} +Next: {{ next_post.title }} +{% endif %} {% endif %} diff --git a/blog/views.py b/blog/views.py index 8b755a2..23c8e35 100644 --- a/blog/views.py +++ b/blog/views.py @@ -58,7 +58,20 @@ def datepage(request, year, month, day, 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}) + next_post = Post.objects.filter(created_at__gt = post.created_at).order_by('created_at')[0:1] + prev_post = Post.objects.filter(created_at__lt = post.created_at).order_by('-created_at')[0:1] + + if not next_post: + next_post = None + else: + next_post = next_post[0] + + if not prev_post: + prev_post = None + else: + prev_post = prev_post[0] + + return render(request, 'blog/view.html', {'post': post, 'prev_post': prev_post, 'next_post': next_post}) def codechunk(request, language, slug): chunk = get_object_or_404(CodeChunk, language=language, slug=slug)