Add previous/next article links to blog articles

This commit is contained in:
Gergely Polonkai 2014-06-19 14:25:23 +00:00
parent 0ad611ce23
commit a5445a03e0
2 changed files with 20 additions and 1 deletions

View File

@ -29,4 +29,10 @@
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
{% if prev_post %}
Previous: <a href="{% url 'blog:read' prev_post.created_at.year prev_post.created_at.month prev_post.created_at.day prev_post.slug %}">{{ prev_post.title }}</a>
{% endif %}
{% if next_post %}
Next: <a href="{% url 'blog:read' next_post.created_at.year next_post.created_at.month next_post.created_at.day next_post.slug %}">{{ next_post.title }}</a>
{% endif %}
{% endif %}

View File

@ -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)