Wagtail - 仅在主页上显示三个最新帖子
Wagtail - display the three latest posts only on the homepage
我创建了一个模型来在主页上显示帖子,但只想显示三个最新帖子。我是否需要为此使用分页,或者是否有一个钩子可以代替?
我想我可以使用分页,只是不包括 'next' 按钮,但这看起来有点像 hack,我想以正确的方式做到这一点。
我对 Django 和 Python 还是很陌生,我会继续试验,但如果有人能指出正确的方向,我将不胜感激。
这是主页模型:
from __future__ import unicode_literals
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from blog.models import BlogPage
class HomePage(Page):
def blogs(self):
blogs = BlogPage.objects.all()
blogs = blogs.order_by('-date')
return blogs
这是 BlogPage 模型:
class BlogPage(Page):
body = RichTextField(verbose_name=_('body'), blank=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
date = models.DateField(
_("Post date"), default=datetime.datetime.today,
help_text=_("This date may be displayed on the blog post. It is not "
"used to schedule posts to go live at a later date.")
)
header_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
verbose_name=_('Header image')
)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True, null=True,
limit_choices_to=limit_author_choices,
verbose_name=_('Author'),
on_delete=models.SET_NULL,
related_name='author_pages',
)
search_fields = Page.search_fields + [
index.SearchField('body'),
]
blog_categories = models.ManyToManyField(
BlogCategory, through=BlogCategoryBlogPage, blank=True)
settings_panels = [
MultiFieldPanel([
FieldRowPanel([
FieldPanel('go_live_at'),
FieldPanel('expire_at'),
], classname="label-above"),
], 'Scheduled publishing', classname="publishing"),
FieldPanel('date'),
FieldPanel('author'),
]
def save_revision(self, *args, **kwargs):
if not self.author:
self.author = self.owner
return super(BlogPage, self).save_revision(*args, **kwargs)
def get_absolute_url(self):
return self.url
def get_blog_index(self):
# Find closest ancestor which is a blog index
return self.get_ancestors().type(BlogIndexPage).last()
def get_context(self, request, *args, **kwargs):
context = super(BlogPage, self).get_context(request, *args, **kwargs)
context['blogs'] = self.get_blog_index().blogindexpage.blogs
context = get_blog_context(context)
context['COMMENTS_APP'] = COMMENTS_APP
return context
class Meta:
verbose_name = _('Blog page')
verbose_name_plural = _('Blog pages')
parent_page_types = ['blog.BlogIndexPage']
BlogPage.content_panels = [
FieldPanel('title', classname="full title"),
MultiFieldPanel([
FieldPanel('tags'),
InlinePanel('categories', label=_("Categories")),
], heading="Tags and Categories"),
ImageChooserPanel('header_image'),
FieldPanel('body', classname="full"),
]
...这是 HTML:
<div class="row">
<div class="col-md-10 col-md-offset-1 blocks home-page-posts">
{% for blog in page.blogs %}
<div class="col-md-4">
<a class="blog-post-link" href="{% pageurl blog %}">
<h3>{{ blog.title }}</h3>
</a>
<div class="blog-intro">
{{ blog.body|richtext|truncatewords_html:50 }}
<a class="read-more" href="{% pageurl blog %}">Read More »</a>
</div>
</div>
{% endfor %}
</div>
</div>
如 https://docs.djangoproject.com/en/1.11/topics/db/queries/#limiting-querysets 中所述,您可以使用数组切片语法来限制查询集:
class HomePage(Page):
def blogs(self):
blogs = BlogPage.objects.all()
blogs = blogs.order_by('-date')[:3]
return blogs
对于 wagtail
有一些默认字段,例如:first_published_at
、last_published_at
、latest_revision_created_at
。在 official doc
中查看更多信息
class HomePage(Page):
# get recent blogs (wagtail has by default first_published_at field)
def get_recent_blogs(self):
max_count = 5 # max count for displaying post
return BlogPage.objects.all().order_by('-first_published_at')[:max_count]
# add this to custom context
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['blogs'] = self.get_recent_blogs()
return context
我创建了一个模型来在主页上显示帖子,但只想显示三个最新帖子。我是否需要为此使用分页,或者是否有一个钩子可以代替?
我想我可以使用分页,只是不包括 'next' 按钮,但这看起来有点像 hack,我想以正确的方式做到这一点。
我对 Django 和 Python 还是很陌生,我会继续试验,但如果有人能指出正确的方向,我将不胜感激。
这是主页模型:
from __future__ import unicode_literals
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from blog.models import BlogPage
class HomePage(Page):
def blogs(self):
blogs = BlogPage.objects.all()
blogs = blogs.order_by('-date')
return blogs
这是 BlogPage 模型:
class BlogPage(Page):
body = RichTextField(verbose_name=_('body'), blank=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
date = models.DateField(
_("Post date"), default=datetime.datetime.today,
help_text=_("This date may be displayed on the blog post. It is not "
"used to schedule posts to go live at a later date.")
)
header_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
verbose_name=_('Header image')
)
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
blank=True, null=True,
limit_choices_to=limit_author_choices,
verbose_name=_('Author'),
on_delete=models.SET_NULL,
related_name='author_pages',
)
search_fields = Page.search_fields + [
index.SearchField('body'),
]
blog_categories = models.ManyToManyField(
BlogCategory, through=BlogCategoryBlogPage, blank=True)
settings_panels = [
MultiFieldPanel([
FieldRowPanel([
FieldPanel('go_live_at'),
FieldPanel('expire_at'),
], classname="label-above"),
], 'Scheduled publishing', classname="publishing"),
FieldPanel('date'),
FieldPanel('author'),
]
def save_revision(self, *args, **kwargs):
if not self.author:
self.author = self.owner
return super(BlogPage, self).save_revision(*args, **kwargs)
def get_absolute_url(self):
return self.url
def get_blog_index(self):
# Find closest ancestor which is a blog index
return self.get_ancestors().type(BlogIndexPage).last()
def get_context(self, request, *args, **kwargs):
context = super(BlogPage, self).get_context(request, *args, **kwargs)
context['blogs'] = self.get_blog_index().blogindexpage.blogs
context = get_blog_context(context)
context['COMMENTS_APP'] = COMMENTS_APP
return context
class Meta:
verbose_name = _('Blog page')
verbose_name_plural = _('Blog pages')
parent_page_types = ['blog.BlogIndexPage']
BlogPage.content_panels = [
FieldPanel('title', classname="full title"),
MultiFieldPanel([
FieldPanel('tags'),
InlinePanel('categories', label=_("Categories")),
], heading="Tags and Categories"),
ImageChooserPanel('header_image'),
FieldPanel('body', classname="full"),
]
...这是 HTML:
<div class="row">
<div class="col-md-10 col-md-offset-1 blocks home-page-posts">
{% for blog in page.blogs %}
<div class="col-md-4">
<a class="blog-post-link" href="{% pageurl blog %}">
<h3>{{ blog.title }}</h3>
</a>
<div class="blog-intro">
{{ blog.body|richtext|truncatewords_html:50 }}
<a class="read-more" href="{% pageurl blog %}">Read More »</a>
</div>
</div>
{% endfor %}
</div>
</div>
如 https://docs.djangoproject.com/en/1.11/topics/db/queries/#limiting-querysets 中所述,您可以使用数组切片语法来限制查询集:
class HomePage(Page):
def blogs(self):
blogs = BlogPage.objects.all()
blogs = blogs.order_by('-date')[:3]
return blogs
对于 wagtail
有一些默认字段,例如:first_published_at
、last_published_at
、latest_revision_created_at
。在 official doc
class HomePage(Page):
# get recent blogs (wagtail has by default first_published_at field)
def get_recent_blogs(self):
max_count = 5 # max count for displaying post
return BlogPage.objects.all().order_by('-first_published_at')[:max_count]
# add this to custom context
def get_context(self, request):
context = super(HomePage, self).get_context(request)
context['blogs'] = self.get_recent_blogs()
return context