2
You can pass the latest 5 posts (ordered by creation_date
) to the template using order_by
:
def blog(request):
return render_to_response('home/blog.html', {
'posts': Post.objects.order_by('-creation_date')[:5]
})
The -
preceding creation_date
indicates that the dates returned will be in descending order, ie the most recent date first.
To add the functionality to display the next 5 posts, I would return the 10 most recent Post
s to the template using the method I have suggested, and then manipulate these using another method (perhaps basic JavaScript),
0
def blog(request):
return render(request, 'home/blog.html', {
'posts': Post.objects.order_by('-creation_date')[:5]
})
render
instead of render_to_response
, which takes care of context instance for you.
Source:stackexchange.com