我正在建立一个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
我还想在主页上显示最新的三篇文章,但是我不确定如何将它们从网站的博客部分传递到主页。我正在尝试做这样的事情:
{% 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
页面上没有显示任何内容。我做错了什么?
作为标准,页面对象在变量名称为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):
为了补充@gasman的答案,您可以通过以下方式获取最新的三篇博客文章:
blogs = ResourcePage.objects.all().order_by('-date')[:3]
注:'-date'
按降序排序。使用'date'
按最早的顺序排序。
注意2:使用live()
而不是all()
来获取实际发布的博客文章