这是我的 model.py 文件
...
class LinkFields(models.Model):
link_external = models.URLField(
"External link",
blank=True,
null=True,
help_text='Set an external link if you want to describe the event from an other web site',
)
link_page = models.ForeignKey(
'wagtailcore.Page',
null=True,
on_delete=models.SET_NULL,
blank=True,
related_name='+',
help_text='Choose an existing page (event must have already been created)',
)
@property
def link(self):
if self.link_page:
return self.link_page.url
else:
return self.link_external
panels = [
FieldPanel('link_external'),
PageChooserPanel('link_page'),
]
class Meta:
abstract = True
class RelatedLink(LinkFields):
title = models.CharField(max_length=255, help_text="Link title")
panels = [
FieldPanel('title'),
MultiFieldPanel(LinkFields.panels, "Link"),
]
class Meta:
abstract = True
class HomePage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
InlinePanel('related_links', label="Related events"),
]
class EventsPage(Page):
# Database fields
date = models.DateField("Event date")
topicTag = models.CharField(max_length = 25)
name = models.CharField(max_length = 50)
city = models.CharField(max_length = 20, default="Where it is")
place = models.CharField(max_length = 20, blank=True)
body = RichTextField(blank=True)
# Search index configuration
search_fields = Page.search_fields + (
index.SearchField('topicTag'),
index.SearchField('place'),
index.FilterField('date'),
)
# Editor panels configuration
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('topicTag'),
FieldPanel('name', classname="title"),
FieldPanel('city'),
FieldPanel('place'),
FieldPanel('body'),
]
class EventsRelatedLink(Orderable, RelatedLink):
page = ParentalKey('HomePage', related_name='related_links')
EventsPages 是 HomePage 的子页面,HomePage 本身显示后续子页面的链接。我的问题是对于我在主页上显示的每个子页面的链接,我想访问所谓的子页面的属性以显示一种预览。
这是我的 home_page.html 模板:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block body_class %}template-homepage{% endblock %}
{% block content %}
<h1>THE <span class="pink">OPEN SOURCE</span> INNOVATION SPRING</h1>
<div class="intro">{{ page.body|richtext }}</div>
{% if page.related_links.all %}
<ul>
{% for item in page.related_links.all %}
<li><a href="{{ item.link }}">{{ item.title }}</a></li>
<br><div class="description">{{ item.date }}
<span class="pink">[{{ item.topicTag }}]</span>
{{ item.name }} <span class="pink">[{{ item.city }}]</span>
</div>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
当然,所有带有 * 作为 EventsPage 模型属性的“item.*”都不起作用。我很确定我走错了路,但我需要帮助才能做到这一点。