我有这样的设置,我的 Orderable 允许我添加多张卡片来显示 pdf 文档。但是,有没有一种方法可以在 InLinePanel 中显示可订购的内容?
例如,我想要 20 张卡片,但在这些卡片中,pdf 的数量范围为 1 到 10 之间的任何数字。我想要这个,因为它使得pdf 很容易找到并且非常易于操作。
class ArchitectPage(Page):
search_fields = Page.search_fields + [
] # these are if adding a search to the website
# content tab panels
content_panels = Page.content_panels + [
MultiFieldPanel(
[InlinePanel('architect_pdf', max_num=20, min_num=0, label="architect doc")],
heading="architect pdf"
),
]
# what to call the panels on wagtail
edit_handler = TabbedInterface([
ObjectList(content_panels, heading='Content'),
ObjectList(Page.promote_panels, heading='SEO'),
ObjectList(Page.settings_panels, heading='Settings', classname='settings'),
# classname settings adds the cog
])
class ArchitectDownloads(Orderable):
page = ParentalKey(ArchitectPage, on_delete=models.CASCADE, related_name='architect_pdf')
architect_pdf = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.CASCADE,
related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
DocumentChooserPanel('architect_pdf'),
]
{% for download in page.architect_pdf.all %}
<div class="document line-up-card">
<div class="w3-card-4 w3-margin w3-white" data-aos="fade-down">
{% image download.image fill-150x150-c100 %}
{% with doc=download.architect_pdf %}
<div class="w3-container">
{{ doc.title }}
</div>
{% endwith %}
<hr>
<p id="caption">{{ download.caption }}</p>
{% with doc=download.architect_pdf %}
<div class="download">
<a href="{{ doc.url }}" class="smooth-over-button noDecoration">
<i class="fa fa-download"></i>
<p class="btn-txt">PDF</p>
</a>
</div>
{% endwith %}
</div>
</div>
{% endfor %}
虽然你不能直接在 Wagtail 中将一个 Orderable 嵌套在另一个 Orderable 中,但你可以通过调整数据模型和使用 InlinePanels 来实现你想要的结果。
以下是如何做到这一点:
1. 修改你的模型:
- 创建一个新的模型来代表一组 PDF 文档。
-
将现有的
ArchitectDownloads
模型重命名为PDFDocument
,并将其与新的组模型相关联。
class ArchitectPage(Page):
# ... 你的现有代码 ...
class PDFDocumentGroup(Orderable):
page = ParentalKey(ArchitectPage, on_delete=models.CASCADE, related_name='pdf_document_groups')
heading = models.CharField(max_length=255, blank=True)
panels = [
FieldPanel('heading'),
InlinePanel('pdf_documents', label="PDF Documents", max_num=10, min_num=1),
]
class PDFDocument(Orderable):
group = ParentalKey(PDFDocumentGroup, on_delete=models.CASCADE, related_name='pdf_documents')
pdf_file = models.ForeignKey(
'wagtaildocs.Document',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.CASCADE,
related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
DocumentChooserPanel('pdf_file'),
]
2. 更新你的模板:
-
迭代
pdf_document_groups
,然后在每个组内迭代pdf_documents
。
{% for group in page.pdf_document_groups.all %}
<h3>{{ group.heading }}</h3>
{% for download in group.pdf_documents.all %}
<div class="document line-up-card">
<div class="w3-card-4 w3-margin w3-white" data-aos="fade-down">
{% image download.image fill-150x150-c100 %}
<div class="w3-container">
{{ download.pdf_file.title }}
</div>
<hr>
<p id="caption">{{ download.caption }}</p>
<div class="download">
<a href="{{ download.pdf_file.url }}" class="smooth-over-button noDecoration">
<i class="fa fa-download"></i>
<p class="btn-txt">PDF</p>
</a>
</div>
</div>
</div>
{% endfor %}
{% endfor %}
通过这种方式,你可以在每个
PDFDocumentGroup
中拥有 1 到 10 个
PDFDocument
,并使用 Wagtail 的 Orderable 功能轻松地对组和组内的文档进行排序。