我正在构建一个 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 %}
博客模型.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):
关于django - 在 Wagtail 的主页上显示博客文章,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40386838/