Wagtail - 仅在主页上显示三个最新帖子 [英] Wagtail - display the three latest posts only on the homepage
问题描述
我创建了一个模型来显示主页上的帖子,但只想显示三个最新的帖子.我需要为此使用分页,还是可以使用钩子代替?
我想我可以使用分页而不包括下一步"按钮,但这似乎有点像黑客,我想以正确的方式做到这一点.
我对 Django 和 Python 仍然很陌生,我将继续试验,但如果有人能指出我正确的方向,我将不胜感激.
这是主页模型:
from __future__ import unicode_literals从 django.db 导入模型从 wagtail.wagtailcore.models 导入页面从 wagtail.wagtailcore.fields 导入 RichTextField从 wagtail.wagtailadmin.edit_handlers 导入 FieldPanel从 blog.models 导入 BlogPage类主页(页面):定义博客(自我):博客 = BlogPage.objects.all()blogs = blogs.order_by('-date')返回博客
这是博客页面模型:
class BlogPage(Page):body = RichTextField(verbose_name=_('body'), blank=True)标签 = ClusterTaggableManager(through=BlogPageTag, blank=True)日期 = 模型.DateField(_("发布日期"), default=datetime.datetime.today,help_text=_("这个日期可能会显示在博文上,不是"用于安排帖子在以后发布."))header_image = models.ForeignKey('wagtailimages.Image',空=真,空白=真,on_delete=models.SET_NULL,相关名称='+',verbose_name=_('标题图片'))作者 = 模型.ForeignKey(settings.AUTH_USER_MODEL,空白=真,空=真,limit_choices_to=limit_author_choices,verbose_name=_('作者'),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 = [多字段面板([字段行面板([FieldPanel('go_live_at'),FieldPanel('expire_at'),], 类名="标签在上面"),], '预定发布', classname="发布"),FieldPanel('日期'),FieldPanel('作者'),]def save_revision(self, *args, **kwargs):如果不是 self.author:self.author = self.ownerreturn super(BlogPage, self).save_revision(*args, **kwargs)def get_absolute_url(self):返回 self.urldef get_blog_index(self):# 找到最近的祖先,即博客索引返回 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上下文 = get_blog_context(上下文)上下文['COMMENTS_APP'] = COMMENTS_APP返回上下文元类:verbose_name = _('博客页面')verbose_name_plural = _('博客页面')parent_page_types = ['blog.BlogIndexPage']BlogPage.content_panels = [FieldPanel('title', classname="full title"),多字段面板([FieldPanel('标签'),InlinePanel('categories', label=_("Categories")),], 标题="标签和类别"),ImageChooserPanel('header_image'),FieldPanel('body', classname="full"),]
...这是 HTML:
<div class="col-md-10 col-md-offset-1 阻止主页帖子">{% 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 %}">阅读更多»</a>
{% 结束为 %}
如 https://docs.djangoproject.com/en/1.11/topics/db/queries/#limiting-querysets ,你可以使用数组切片语法来限制查询集:>
class HomePage(Page):定义博客(自我):博客 = BlogPage.objects.all()blogs = blogs.order_by('-date')[:3]返回博客
I have created a model to show posts on the homepage, but only want the three latest posts to show. Do I need to use pagination for this, or is there a hook I can use instead?
I was thinking I could use pagination and just not include the 'next' button, but that seems somewhat like a hack, and I want to do this the right way.
I am still very new to Django and Python and am going to continue experimenting, but if someone could point me in the right direction I would be very grateful.
Here's the HomePage model:
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
Here's the BlogPage model:
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"),
]
...and here's the 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>
As documented at https://docs.djangoproject.com/en/1.11/topics/db/queries/#limiting-querysets , you can use array-slicing syntax to limit the queryset:
class HomePage(Page):
def blogs(self):
blogs = BlogPage.objects.all()
blogs = blogs.order_by('-date')[:3]
return blogs
这篇关于Wagtail - 仅在主页上显示三个最新帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!