在Wagtail的主页上显示博客文章

时间:2016-11-02 18:06:22

标签: django blogs wagtail

我正在建立一个在博客索引页面上列出博客帖子的Wagtail网站,但我也想在网站的主页上显示最新的三个帖子。我目前在博客索引页面上显示博客的代码如下:

blog_index_page.html:

{% for blog in blogs %}
  <div class="col-sm-12">
    <div class="blog-post post-format-image">
      <div class="blog-post-side">
        <div class="blog-post-date">
          <p class="date-day">{{ blog.date.day }}</p>
          <p class="date-month">{{ blog.date|date:"M" }}</p>
        </div>
      </div>

      <div class="blog-post-content">
        <div class="post-media">
          {% image blog.main_image width-1225 %}
        </div>
      <div class="post-info">
        <h3 class="post-title"><a href="{{ blog.slug }}">{{ blog.title }}</a></h3>
      </div>
      <div class="post-content">
        <p>{{ blog.intro }}</p>
      </div>
      <a class="btn btn-primary btn-sm" href="{{ blog.slug }}">Read More<i class="fa iconright fa-arrow-circle-right"></i></a>
    </div>
  </div>
{% endfor %}

Blog models.py

class ResourcePage(Page):
    main_image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )
    date = models.DateField(blank=True, null=True)
    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'),
        ImageChooserPanel('main_image'),
        FieldPanel('intro'),
        FieldPanel('body', classname='full'),
    ]

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

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

    @property
    def blogs(self):
        # Get list of live blog pages that are descendants of this page
        blogs = ResourcePage.objects.live().descendant_of(self)

        # Order by most recent date first
        blogs = blogs.order_by('date')

        return blogs

    def get_context(self, request):
        # Get blogs
        blogs = self.blogs

        # Filter by tag
        tag = request.GET.get('tag')
        if tag:
            blogs = blogs.filter(tags__name=tag)

        # Pagination
        page = request.GET.get('page')
        paginator = Paginator(blogs, 5)  # Show 5 blogs per page
        try:
            blogs = paginator.page(page)
        except PageNotAnInteger:
            blogs = paginator.page(1)
        except EmptyPage:
            blogs = paginator.page(paginator.num_pages)

        # Update template context
        context = super(ResourceIndexPage, self).get_context(request)
        context['blogs'] = blogs
        return context

    def get_sitemap_urls(self):
        return [
            {
                'location': self.full_url,
                'lastmod': self.latest_revision_created_at,
                'changefreq': 'monthly',
                'priority': .5
            }
        ]

    ResourceIndexPage.content_panels = [
        FieldPanel('title', classname="full title"),
        FieldPanel('intro', classname="full"),
        InlinePanel('related_links', label="Related links"),
    ]

    ResourceIndexPage.promote_panels = Page.promote_panels

我还希望在主页上显示最新的三个帖子,但我不确定如何将它们从网站的博客部分传递到主页。我试图做这样的事情:

{% for blog in blogs %}
  <div class="carousel-item">
    <a href="/education/{{ blog.slug }}">{% image blog.main_image width-360 %}</a>
    <div class="panel panel-default">
      <div class="panel-body">
        <h5><a href="blog-single-post.html">{{ blog.title }}</a></h5>
        <p>{{ blog.intro }}</p>
      </div>
    </div>
 </div>
{% endfor %}

主页models.py

from __future__ import absolute_import, unicode_literals

from django.db import models
from django.http import HttpResponseRedirect

from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField, StreamField
from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.blocks import URLBlock, DateBlock
from wagtail.wagtaildocs.edit_handlers import DocumentChooserPanel
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.wagtailimages.blocks import ImageChooserBlock

from resources.models import ResourcePage


class HomePage(Page):

    def blogs(ResourcePage):
        # Get list of live blog pages that are descendants of the ResourceIndexPage page
        blogs = ResourcePage.objects.all()

        # Order by most recent date first
        blogs = resources.order_by('date')

        return blogs

页面上没有任何内容显示。我做错了什么?

2 个答案:

答案 0 :(得分:4)

作为标准,页面对象在变量名page下的模板中可用。 (您可能还看到self被使用,但page是首选,因为它与其他模板引擎(如Jinja2)兼容。)这意味着如果您定义blogs在页面模型上的方法,您可以page.blogs

访问它
{% for blog in page.blogs %}

如果您只想将其称为blogs,则需要定义一个get_context方法,明确将其添加到模板上下文中:

class HomePage(Page):
    ...
    def get_context(self, request):
        context = super(HomePage, self).get_context(request)
        context['blogs'] = self.blogs()
        return context

此外,行def blogs(ResourcePage):不正确 - 您将其与类定义混淆。这应该是:def blogs(self):

答案 1 :(得分:1)

要添加@gasman的答案,以下是您获取最新三篇博文的方法:

for (String key : myError.message.keySet()) { Utils.showToast(getBaseContext(), "Message: " + myError.message.get(key) + " Field: " + key); }

注意:blogs = ResourcePage.objects.all().order_by('-date')[:3]命令作为降序列表。使用'-date'按最早排序。

注意2:使用'date'代替live()仅获取实际发布的博文。