How I display wagtail blog post in other pages like homepage

Viewed 217

Noob here. So as the title I'm trying to display blog around my pages. What I'm trying to achive is:

  • homepage who has a list of the latest post.
  • Blog index Page.
  • Blog category pages.
  • a sidebar to use in my other pages.

So that's has been my step so far:

  • set up a wagtail project;
  • install wagtail-blog;
  • moved blog folder to Lib in my root folder
  • set up root, template, and staticfolder.

As I know by now my blog will show up only in my blog_index_page.html as a http:127.0.0.1/blog/ and blogpost showing up in 127.0.0.1/blog/blog-post-name. So blog looks like working fine.

I'm really struggling to unbderstand how to implement the latest post in my homepage; making a category blog page and the sidebar who display latestpost around my site.

so that's my code:

blog/models.py

from django.core.exceptions import ValidationError

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

from django.conf import settings

from django.contrib.auth import get_user_model

from django.db.models import Count

from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404
from django.template.defaultfilters import slugify
from wagtail.snippets.models import register_snippet
from taggit.models import Tag
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel

from .abstract import (
    BlogCategoryAbstract,
    BlogCategoryBlogPageAbstract,
    BlogIndexPageAbstract,
    BlogPageAbstract,
    BlogPageTagAbstract
)


COMMENTS_APP = getattr(settings, 'COMMENTS_APP', None)


class BlogIndexPage(BlogIndexPageAbstract):
    class Meta:
        verbose_name = _('Blog index')

    @property
    def blogs(self):
        # Get list of blog pages that are descendants of this page
        blogs = BlogPage.objects.descendant_of(self).live()
        blogs = blogs.order_by(
            '-date'
        ).select_related('owner').prefetch_related(
            'tagged_items__tag',
            'categories',
            'categories__category',
        )
        return blogs

    def get_context(self, request, tag=None, category=None, author=None, *args,
                    **kwargs):
        context = super(BlogIndexPage, self).get_context(
            request, *args, **kwargs)
        blogs = self.blogs

        if tag is None:
            tag = request.GET.get('tag')
        if tag:
            blogs = blogs.filter(tags__slug=tag)
        if category is None:  # Not coming from category_view in views.py
            if request.GET.get('category'):
                category = get_object_or_404(
                    BlogCategory, slug=request.GET.get('category'))
        if category:
            if not request.GET.get('category'):
                category = get_object_or_404(BlogCategory, slug=category)
            blogs = blogs.filter(categories__category__name=category)
        if author:
            if isinstance(author, str) and not author.isdigit():
                blogs = blogs.filter(author__username=author)
            else:
                blogs = blogs.filter(author_id=author)

        # Pagination
        page = request.GET.get('page')
        page_size = 10
        if hasattr(settings, 'BLOG_PAGINATION_PER_PAGE'):
            page_size = settings.BLOG_PAGINATION_PER_PAGE

        paginator = None
        if page_size is not None:
            paginator = Paginator(blogs, page_size)  # Show 10 blogs per page
            try:
                blogs = paginator.page(page)
            except PageNotAnInteger:
                blogs = paginator.page(1)
            except EmptyPage:
                blogs = paginator.page(paginator.num_pages)

        context['blogs'] = blogs
        context['category'] = category
        context['tag'] = tag
        context['author'] = author
        context['COMMENTS_APP'] = COMMENTS_APP
        context['paginator'] = paginator
        context = get_blog_context(context)

        return context

    subpage_types = ['blog.BlogPage']


@register_snippet
class BlogCategory(BlogCategoryAbstract):
    class Meta:
        ordering = ['name']
        verbose_name = _("Blog Category")
        verbose_name_plural = _("Blog Categories")


class BlogCategoryBlogPage(BlogCategoryBlogPageAbstract):
    class Meta:
        pass


class BlogPageTag(BlogPageTagAbstract):
    class Meta:
        pass


@register_snippet
class BlogTag(Tag):
    class Meta:
        proxy = True


def get_blog_context(context):
    """ Get context data useful on all blog related pages """
    context['authors'] = get_user_model().objects.filter(
        owned_pages__live=True,
        owned_pages__content_type__model='blogpage'
    ).annotate(Count('owned_pages')).order_by('-owned_pages__count')
    context['all_categories'] = BlogCategory.objects.all()
    context['root_categories'] = BlogCategory.objects.filter(
        parent=None,
    ).prefetch_related(
        'children',
    ).annotate(
        blog_count=Count('blogpage'),
    )
    return context


class BlogPage(BlogPageAbstract):
    class Meta:
        verbose_name = _('Blog page')
        verbose_name_plural = _('Blog pages')

    def get_blog_index(self):
        # Find closest ancestor which is a blog index
        return self.get_ancestors().type(BlogIndexPage).last()

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)
        context['blogs'] = self.get_blog_index().blogindexpage.blogs
        context = get_blog_context(context)
        context['COMMENTS_APP'] = COMMENTS_APP
        return context

    parent_page_types = ['blog.BlogIndexPage']
'''


blog/models.py

from django.db import models

from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel




class HomePage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),
    ]


def get_context(self, request):
        context = super().get_context(request)

        # Add extra variables and return the updated context
        # note: be sure to get the `live` pages so you do not show draft pages
        context['blog_pages'] = BlogPage.objects.child_of(self).live()
        return context

class AboutMe(Page):
    body = RichTextField(blank=True)

    template = 'blog/blog_index_page.html'

    content_panels = Page.content_panels + [
        FieldPanel('body', classname="full"),

that's the for loop I'm trying to display the latest post


             {% block content %}
              <section id="section-personal-blog" data-bgcolor="#f5f5f5">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6 offset-md-3 text-center">
                            <h2>Latest Post</h2>
                            <div class="small-border"></div>
                        </div>

                        <div class="col-md-12">
                            <div id="blog-carousel" class="owl-carousel owl-theme">
                                <div class="bloglist item">
                                    <div class="post-content">
                                        <div class="post-image">
                                            <img src="{% static 'images/blog-2/pic-blog-1.jpg' %}" alt="" />
                                        </div>

                                        <div class="post-text">
                                        {% include blog/blog_post.html' with blog=self %}
                                        </div>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>

            </section>
            {% endblock content %}

Thanks in advice for your time reading this and help me!

0 Answers
Related