2017-08-03 125 views
2

我在我的网站,主页,博客索引和博客特定3个主要部分。我在w using中使用流场功能来订购主页中的各个部分。其中一个部分是最新的三篇博文。W Show在主页上显示最新博客文章通过streamfield

我已经为博客索引页做了这个,但不能抓住流场中的最新博客文章。

我的模型看起来像这样

class CaseStudiesIndex(Page): 
    def casestudies(pages): 
     casestudies = CaseStudyPage.objects.all().order_by('-first_published_at') 
     return casestudies 
    intro = RichTextField(blank=True) 
    content_panels = Page.content_panels + [ 
     FieldPanel('intro', classname="full") 
    ] 

class LatestPosts(blocks.StructBlock): 
    static = blocks.StaticBlock(admin_text='Latest posts: no configuration needed.',) 
    def casestudies(pages): 
     casestudies = CaseStudyPage.objects.all().order_by('-first_published_at')[:3] 
     return casestudies 

    class Meta: 
     icon = 'doc-full' 
     label = 'Latest Posts' 
     template = 'blocks/_latestPosts.html' 

class HomePage(Page): 
    blocksbody = StreamField([ 
     ('lead_banner', LeadBanner()), 
     ('latest_posts', LatestPosts()), 
     ('team', Team()) 
    ],null=True,blank=True) 


    content_panels = Page.content_panels + [ 
     StreamFieldPanel('blocksbody'), 
    ] 

在我的块文件夹,我调用文件罚款,它呈现的包装不错,但我不能抢的任何数据,我已经尝试了一堆方式但没有回报。

{% load wagtailcore_tags wagtailimages_tags %} 
{% load static %} 

<section> 
    <div class="wrapper__inner"> 
     <ul> 
      {% for case in self.casestudies %} 
       {{case.title}} 
      {% endfor %} 

      {% for case in self.case_studies %} 
       {{case.title}} 
      {% endfor %} 

      {% for case in self.latest_posts %} 
       {{case.title}} 
      {% endfor %} 

      {% for case in page.casestudies %} 
       {{case.title}} 
      {% endfor %} 

      {% for case in page.case_studies %} 
       {{case.title}} 
      {% endfor %} 

      {% for case in page.latest_posts %} 
       {{case.title}} 
      {% endfor %} 
     </ul> 
    </div> 
</section> 

对于可以工作的Blog Index页面,我执行以下操作。

{% extends "inner_base.html" %} 
{% load wagtailcore_tags %} 
{% block body_class %}template-case-studies{% endblock %} 
{% block content %} 
    <section> 
     <div class="wrapper__inner"> 
      <h1>{{self.title}}</h1> 
      <ul> 
       {% include "blocks/CaseStudiesLatestBlock.html" %} 
      </ul> 
     </div> 
    </section> 
{% endblock %} 

和正常工作的样子

{% load wagtailcore_tags wagtailimages_tags %} 
{% load static %} 
{% for case in self.casestudies %} 
    <li> 
     <strong>{{ case.title }}</strong> 
    </li> 
{% endfor %} 

回答

1

定义一个StructBlock自己的方法是行不通的CaseStudiesLatestBlock.html - 您收到模板上的self(或value)变量只是一个普通的字典,而不是StructBlock对象本身。 (这似乎是违反直觉的,但它与块的一般工作原理一致:就像CharBlock给你一个字符串值,而不是一个CharBlock实例工作,StructBlock给你一个字典,而不是一个StructBlock实例)

相反,你可以定义一个get_context方法(as documented here)提供额外的变量到模板:

class LatestPosts(blocks.StructBlock): 
    static = blocks.StaticBlock(admin_text='Latest posts: no configuration needed.',) 

    def get_context(self, value, parent_context=None): 
     context = super(LatestPosts, self).get_context(value, parent_context=parent_context) 
     context['casestudies'] = CaseStudyPage.objects.all().order_by('-first_published_at')[:3] 
     return context 

然后,您可以访问casestudies变量模板,例如{% for case in casestudies %}

+0

作品感谢! – PeterGabriel

相关问题