ブログの投稿ページがブログのインデックスページに表示されているWagtailサイトを構築していますが、ウェブサイトのホームページに最新の3つの投稿も表示したいと思います。次のようにブログのインデックスページ上のブログを表示するために私の現在のコードは次のとおりです。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 %}
ブログ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
私はまた、最新の3件の記事を表示したいですホームページのブログセクションからホームページにそれらを渡す方法がわかりません。
{% 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
何がページ上に表示されていない:私はこのような何かをしようとしています。私は間違って何をしていますか?
変数 'blogs'はあなたのホームページ(HomePageモデルかテンプレート自体のいずれか)のどこにでも定義しましたか?その場合は、そのコードを共有してください。 – gasman
申し訳ありません、私はちょうど上記の私のメインポストに追加しました。ありがとう! – Kim