Search code examples
pythondjangocontent-management-systemmezzanine

How could I get the latest blog posts for Mezzanine page?


I'm new on Mezzanine and I want to display the 8 latest posts on a custom section of the home page.

I already built the QuerySet: BlogPost.objects.filter(publish_date__isnull=False).order_by('-publish_date')[:8]

I've already checked templates/blog/blog_post_list.html but I'm not clear on how can I pass the QuerySet result to the view.


Solution

  • I found an answer Fetch blog entries with bootstrap custom theme and mezzanine. You can use the blog_recent_posts tag from blog_tags. Load the tags at the beginning:

    {% load blog_tags %}

    And where you want to iterate the recent posts:

    <ul>
      {% blog_recent_posts as recent_posts %}
      {% for blog_post in recent_posts %}
        <li>{{ blog_post.title }}</li>
      {% endfor %}
    </ul>