[我将跟随tail的Your First Wagtail Site,特别是在BlogIndexPage中的每个帖子旁边添加主图像作为缩略图。在添加自定义HTML之前,一切工作正常。
Models.py:
class BlogIndexPage(Page):
templates = "blog/blog_index_page.html"
max_count = 1
intro = RichTextField(blank=True)
def get_context(self, request):
# Update context to include only published guides, ordered by reverse-chron
context = super().get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
context['blogpages'] = blogpages
return context
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
class BlogPage(Page):
templates = "blog/blog_page.html"
date = models.DateField("Post date")
intro = models.CharField(max_length=250, blank=True, null=True)
body = RichTextField(blank=True, null=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
categories = ParentalManyToManyField('blogs.BlogCategory', blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
], heading="Blog information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
]
blog_index_page.html:(使用Wagtail的示例HTML一切都正常,但是一旦添加了自己的自定义HTML,图像就停止了工作。Inspect Element显示图像.jpgs的来源正确,它们只是不起作用。 t显示出来。URL,标题和介绍正常。)
{% load wagtailcore_tags wagtailimages_tags %}
<div id="posts" class="post-grid grid-container clearfix" data-layout="fitRows">
{% for post in page.get_children %} {% with post=post.specific %}
<div class="entry clearfix">
<div class="entry-image">
{% with post.main_image as main_image %} {% if main_image %}
<a href="{{ main_image.url }}" data-lightbox="image"
><img
class="image_fade"
src="{{ main_image.url }}"
alt="{{ main_image.alt }}"
/></a>
{% endif %} {% endwith %}
</div>
<div class="entry-title">
<h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
</div>
<div class="entry-content">
<p>{{ post.intro|richtext }}</p>
<a href="{% pageurl post %}" class="more-link">Read More</a>
</div>
</div>
{% endwith %} {% endfor %}
</div>
似乎您没有在使用wagtailimages_tags导入/声明图像。在开始使用实际图像之前,应该已经为此添加了一些内容:{%image post.main_image%}