eudicots / Cactus

Static site generator for designers. Uses Python and Django templates.
BSD 3-Clause "New" or "Revised" License
3.46k stars 314 forks source link

Showing Only 3 most recent posts #247

Closed ciaranha closed 8 years ago

ciaranha commented 8 years ago

I'm using the portfolio template and wondering how I would go about displaying only the 3 most recent projects or posts on the index page?

It obviously currently outputs all of these with {% for post in posts %}...{% endfor %}

I've tried adding `def index(request):

get the blog posts that are published

posts = Post.objects.filter(published=True).order_by('-pub_date')[0:3]
# now return the rendered template
return render(request, '/', {'post': posts})` 

into blog.py but I'm not getting any results!

ghost commented 8 years ago

You could use the slice notation: {% for post in posts|slice:":4" %}

Take a look at the accepted answer of this stackoverflow question for a good explanation: Explain Python's slice notation

ghost commented 8 years ago

@Jaibles could you please close the issue if the above answer solves your problem? Just to keep the issue tracker clean. Thanks!