I am new to Django and wagtail, I trying to build a project blog website with wagtail,
Project listing page(project_listing_page.html
) template is not showing images on my first wagtail site, I am trying to have thumbnails as the first image of project_page.html
page.
please help...
thanks in advance...
Code details as below...
in models.py
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.core.models import Page, Orderable
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel, InlinePanel
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.search import index
class ProjectIndexPage(Page):
intro = RichTextField(blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
class ProjectPage(Page):
date = models.DateField("Post date")
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'),
FieldPanel('intro'),
FieldPanel('body', classname="full"),
InlinePanel('gallery_images', label="Gallery images"),
]
class ProjectPageGalleryImage(Orderable):
page = ParentalKey(ProjectPage, on_delete=models.CASCADE, related_name='gallery_images')
image = models.ForeignKey(
'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
in Project_index_page.html
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block body_class %} template-projectindexpage {% endblock %}
{% block content %}
<div class="intro">{{ page.intro|richtext }}</div>
<div class="content">
{% image page.ProjectPageGalleryImage max 200x200 %}
<img src="{{ ProjectPageGalleryImage.url }}" alt= "{{ProjectPageGalleryImage.alt}}">
{% for post in page.get_children %}
<h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
{{ post.specific.intro }}
{{ post.specific.body|richtext }}
{% endfor %}
</div>
{% endblock %}
in project_page.html (project detail page template)
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block body_class %}template-blogpage{% endblock %}
{% block content %}
{% for item in page.gallery_images.all %}
<div style="float: left; margin: 10px">
{% image item.image fill-840x240 %}
<p>{{ item.caption }}</p>
</div>
{% endfor %}
<h1>{{ page.title }}</h1>
<p class="meta">{{ page.date }}</p>
<div class="intro">{{ page.intro }}</div>
{{ page.body|richtext }}
<p><a href="{{ page.get_parent.url }}">Return to blog</a></p>
{% endblock %}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…