2017-05-09 5 views
0

私のDjango/Wagtail CMSブログの子ページの本文にアクセスしようとしています。私は子ページのタイトルを返すことができますが、それを使って残りの子ページ属性を取得する方法はわかりません。親はIndexPageで、子はIndexListSubPageです。Django/WagtailCMS - get_contextを使用して子ページの属性(本文など)を取得します。

{{ sub_pages }} //returns <QuerySet [<Page: Page title here>]> 
{{ sub_pages.body }} //returns nothing 

これは、子ページのページタイトルを返しますが、私はまた、本文のような他の属性を、したい:私は私のテンプレートで様々な組み合わせを試してみました

class IndexPage(Page): 
    body = RichTextField(blank=True) 
    feed_image = models.ForeignKey(
     'wagtailimages.Image', 
     null=True, 
     blank=True, 
     on_delete=models.SET_NULL, 
     related_name='+' 
    ) 

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

    def get_context(self, request): 
     context = super(IndexPage, self).get_context(request) 
     context['sub_pages'] = self.get_children() 
     return context 

class IndexListSubPage(Page): 
    body = RichTextField(blank=True) 
    feed_image = models.ForeignKey(
     'wagtailimages.Image', 
     null=True, 
     blank=True, 
     on_delete=models.SET_NULL, 
     related_name='+' 
    ) 

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

:私のモデルがあります。何か案は?私はhereから画像テンプレートの設定を試しました - 再び、私はタイトルを得ることができますが、属性はありません。ページには、管理インターフェースにイメージと本文の両方のテキストがあります。

+0

[Wagtail:親ページ内の子ページのリストを表示](http://stackoverflow.com/questions/32429113/wagtail-display-a-list-of-child-pages-inside-a) -parent-page) – gasman

+0

私はその質問で解決策を試しました - タイトル以外の属性は取得できません。 – geonaut

+0

これを解決するために 'self.get_children()'を 'self.get_children()。specific()'に変更することを期待しています。 – gasman

答えて

1

@gasmanが示唆するように、.specific()を含むようにモデルを変更して動作させました。ワーキングモデルは次のとおりです。

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

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

def get_context(self, request): 
    context = super(ProjectsPage, self).get_context(request) 
    context['sub_pages'] = self.get_children().specific() 
    print(context['sub_pages']) 
    return context 

、テンプレートで:

{% with sub_pages as pages %} 
    {% for page in pages %} 
     {{ page.title }} 
     {{ page.body }} 
    {% endfor %} 
{% endwith %} 

タイトルと子ページの本文今レンダリングされています。

関連する問題