0

I am trying to have the PostPage and EditorialPage show on the Blog(IndexPage). I know it has something to do with the fact that it's only calling the EditorialPage because of:

def get_posts(self):
    return EditorialPage.objects.descendant_of(self).live().order_by("-post_date")

But I don't know how to implement multiple pages. I seriously need help on this one. The code is below:

''' BLOG SECTION '''
class BlogPage(RoutablePageMixin, Page):
    banner_image = models.ForeignKey(
    "wagtailimages.Image",
    null=True,
    blank=True,
    on_delete=models.SET_NULL,
    related_name="+"
)

description = models.CharField(max_length=255, blank=True,)

content_panels = Page.content_panels + [
    ImageChooserPanel("banner_image"),
    FieldPanel("description", classname="full"),
    ]

# PAGINATOR 
def get_context(self, request, *args, **kwargs):
    context = super(BlogPage, self).get_context(request, *args, **kwargs) 
    context['blog_page'] = self

    # https://docs.djangoproject.com/en/3.1/topics/pagination/#using-paginator-in-a-view-function
    
    # Paginate all posts by 2 per page
    paginator = Paginator(self.posts, 3) 
    # Try to get the ?page=x value
    page = request.GET.get("page")
    try:
        # If the page exists and the ?page=x is an int
        posts = paginator.page(page) 
    except PageNotAnInteger:
        # If the ?page=x is not an int; show the first page
        posts = paginator.page(1) 
    except EmptyPage:
        posts = paginator.object_list.none()
    
    context['posts'] = posts   
    context['authors'] = BlogAuthor.objects.all() # Loop through all Authors of Single Posts
    return context
# PAGINATOR END

def get_posts(self):
    return EditorialPage.objects.descendant_of(self).live().order_by("-post_date")

@route(r"^(\d{4})/$")
@route(r"^(\d{4})/(\d{2})/$")
@route(r"^(\d{4})/(\d{2})/(\d{2})/$")
def post_by_date(self, request, year, month=None, day=None, *args, **kwargs):
    self.filter_type = 'date'
    self.filter_term = year
    self.posts = self.get_posts().filter(post_date__year=year) 
    if month:
        df = DateFormat(datetime.date(int(year), int(month), 1))
        self.filter_term = df.format('F Y')
        self.posts = self.posts.filter(post_date__month=month)
    if day:
        self.filter_term = date_format(datetime.date(int(year), int(month), int(day))) 
        self.posts = self.posts.filter(post_date__day=day)
    return self.render(request)

@route(r"^(\d{4})/(\d{2})/(\d{2})/(.+)/$")
def post_by_date_slug(self, request, year, month, day, slug, *args, **kwargs):
    post_page = self.get_posts().filter(slug=slug).first() 
    if not post_page:
        raise Http404
    # here we render another page, so we call the serve method of the page instance 
    return post_page.serve(request)

@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
    self.filter_type = 'tag'
    self.filter_term = tag
    self.posts = self.get_posts().filter(tags__slug=tag) 
    return self.render(request)

@route(r'^category/(?P<category>[-\w]+)/$')
def post_by_category(self, request, category, *args, **kwargs):
    self.filter_type = 'category'
    self.filter_term = category
    self.posts = self.get_posts().filter(categories__blog_category__slug=category) 
    return self.render(request)

@route(r"^search/$")
def post_search(self, request, *args, **kwargs):
    search_query = request.GET.get("q", None) 
    self.posts = self.get_posts()
    if search_query:
        self.filter_term = search_query
        self.filter_type = 'search'
        self.posts = self.posts.search(search_query)
    return self.render(request)

@route(r'^$')
def post_list(self, request, *args, **kwargs):
    self.posts = self.get_posts() 
    return self.render(request)

''' POST SECTION '''
class PostPage(Page):

...

search_fields = Page.search_fields + [
    index.SearchField('title'),
    index.SearchField('body'),
]

def get_context(self, request, *args, **kwargs):
    context = super().get_context(request, *args, **kwargs) 
    context['blog_page'] = self.blog_page
    return context

@cached_property
def blog_page(self):
    return self.get_parent().specific

@cached_property
def canonical_url(self):
    # we should import here to avoid circular import
    from blog.templatetags.blogapp_tags import post_page_date_slug_url # DO NOT MOVE!

    blog_page = self.blog_page
    return post_page_date_slug_url(self, blog_page)

def save(self, *args, **kwargs):
    key = make_template_fragment_key(
        "blog_post_preview",
        [self.id]
    )
    cache.delete(key)

    return super().save(*args, **kwargs)


''' EDITORIAL SECTION '''
class EditorialPage(Page):

...

# search_fields = Page.search_fields + [
#     index.SearchField('title'),
#     index.SearchField('body'),
# ]

# def get_context(self, request, *args, **kwargs):
#     context = super().get_context(request, *args, **kwargs) 
#     context['blog_page'] = self.blog_page
#     return context

# @cached_property
# def blog_page(self):
#     return self.get_parent().specific

# @cached_property
# def canonical_url(self):
#     # we should import here to avoid circular import
#     from blog.templatetags.blogapp_tags import post_page_date_slug_url # DO NOT MOVE!

#     blog_page = self.blog_page
#     return post_page_date_slug_url(self, blog_page)
Sterl
  • 17
  • 1
  • 6
  • Are PostPage and EditorialPage children of BlogPage? If so, you can use page.get_children.live.public within the template of the BlogPage and it will give you all children of BlogPage that are live and public. – ja408 Feb 04 '22 at 19:30

1 Answers1

0

Are PostPage and EditorialPage children of BlogPage? If so, you can use page.get_children.live.public and order it by post_date in descending order within the template of the BlogPage in a for loop and it will give you all children of BlogPage that are live and public.

{% for post in page.get_children.live.public|dictsort:'-post_date' %}
  {{ post }}
{% endfor %} 

or if you want to add a definition to the BlogPage model:

def get_posts(self):
        self.get_children().live().public().order_by("-post_date")
ja408
  • 798
  • 5
  • 16
  • Exception Value: Cannot resolve keyword 'post_date' into field. Choices are: alias_of, alias_of_id, aliases, blog_authors, blogpage, categories, content_type, content_type_id, depth, documentpage, draft_title, editorialpage, expire_at, expired, first_published_at, flatmenuitem, formpage, formsubmission, go_live_at, group_permissions, has_unpublished_changes, homepage, id, index_entries, last_published_at, latest_revision_created_at, live, live_revision, live_revision_id, locale, locale_id, locked, locked_at, locked_by, locked_by_id, mainmenuitem, numchild, owner, path, post_tags, postpage, – Sterl Feb 04 '22 at 20:15
  • is post_date in both PostPage and Editorial Page? if not, maybe it would make more sense to use first_published_at or last_published_at – ja408 Feb 04 '22 at 20:50