Displaying list of Blog Pages on the Index page

326 views
Skip to first unread message

Leonardo Mateo

unread,
Jan 9, 2016, 9:53:11 PM1/9/16
to Wagtail support
Hi,

I'm unable to get a list of blog pages to display on the index page. In my models.py I have a BlogPage and an IndexPage model, I have created a few blog pages, but when I access the Index page, they are not showing.

Here is the code:


from django.db import models

from modelcluster.fields import ParentalKey

from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index


class IndexPage(Page):
    intro = RichTextField(blank=True)

    def child_pages(self):
        return BlogPage.objects.live().child_of(self)


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

    subpage_types = ['blog.BlogPage']

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)

    search_fields = Page.search_fields + (
        index.SearchField('intro'),
        index.SearchField('body'),
    )

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


And on my template, I have:


{% extends "base.html" %}

{% load wagtailcore_tags %}

{% block body_class %}template-index_page{% endblock %}


{% block content %}

<div class="intro">{{ self.intro|richtext }}</div>

{% for page in self.child_pages %}
  {{ page.title }}
  {{ page.intro }}
  
{% endfor %}
    
{% endblock %}

Could someone clarify what I'm doing wrong here.

Thanks,
Leo-

Matthew Westcott

unread,
Jan 10, 2016, 5:53:04 PM1/10/16
to wag...@googlegroups.com
Hi Leo,
The code you've posted looks correct to me (apart from the content_panels definition of IndexPage being indented too much - but I'm guessing that was a transcription error rather than an actual error in the code). At this point I'd try some debugging on the command line (from your project root, run `./manage.py shell`):

from myapp.models import IndexPage, BlogPage

# you might neeed to change this to something like IndexPage.objects.get(slug='blog')
# if you have several index pages
index_page = IndexPage.objects.get()

index_page.child_pages()

If this returns no results, try variations like BlogPage.objects.live(), BlogPage.objects.all() until you find something that does work. If index_page.child_pages() does return the expected results, then that suggests the template might be at fault - try adding some extra text inside the {% for page in self.child_pages %} block to see if it's entering the loop or not.

Cheers,
- Matt

Leonardo Mateo

unread,
Jan 10, 2016, 8:34:12 PM1/10/16
to wag...@googlegroups.com
Matthew,

Thanks for the assistance, the lines BlogPage.objects.live(), BlogPage.objects.all() worked for me. 

Regards,
-Leo

--
You received this message because you are subscribed to a topic in the Google Groups "Wagtail support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wagtail/6rPgeeDYi4Y/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wagtail+u...@googlegroups.com.
To post to this group, send an email to wag...@googlegroups.com.
Visit this group at https://groups.google.com/group/wagtail.
To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/4AF1FD8F-B656-4298-B23C-0B3A447FA429%40torchbox.com.
For more options, visit https://groups.google.com/d/optout.

Brett Grace

unread,
Jan 11, 2016, 12:32:53 AM1/11/16
to Wagtail support
Are the BlogPage instances actually children of the IndexPage? Just using live() or all()will pick up BlogPages which are children of any page, including ones under different sites and even unpublished ones (in the case of all()).

Leonardo Mateo

unread,
Jan 11, 2016, 8:32:21 AM1/11/16
to wag...@googlegroups.com
Brett,

Thank you for the clarification, I will also look into this.

Regards,
Leo


To post to this group, send email to wag...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages