0
我正在嘗試構建基於W tutorial教程here的片段。未在網頁上顯示的片段
我建立了我的片段在models.py
和創造在templatetags
文件夾中的自定義模板標籤,並通過ForeignKey
連接段到Page
。我也創建了html模板並運行makemigrations
和migrate
。
這個片段是一個標題圖片和一個標題,除了主頁以外的所有頁面上都會出現。代碼如下:
片段型號:
@register_snippet
class HeroImage(models.Model):
text = models.CharField(max_length=255)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
panels = [
FieldPanel('text'),
ImageChooserPanel('image'),
]
def __str__(self):
return self.text
摘錄自定義模板標籤:
@register.inclusion_tag('hero_image.html', takes_context=True)
def hero_images(context):
self = context.get('self')
if self is None or self.depth <= 2:
hero =()
else:
hero = HeroImage.objects.all()
return {
'hero': hero,
'request': context['request'],
}
片段連接到頁:
class MenuPage(Page):
hero = models.ForeignKey(
'home.HeroImage',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
SnippetChooserPanel('hero'),
]
我已經創建了兩個片段,並選擇一個在管理界面的我的頁面中。在模板中,我嘗試了許多不同的Snippet調用,但它們都不返回任何內容。我曾嘗試調用包括:
{{ page.hero }}
{{ page.hero.text }}
{{ hero.text }}
{{ page.hero_images }}
{{ page.hero_images.text }}
Etc...
沒有什麼不幸的工作,我好不容易纔得到的工作是通過鍵入{{ hero }}
的唯一的事這回<QuerySet [<HeroImage: Lorem Ipsum One>, <HeroImage: Lorem Ipsum Two>]>
我在做什麼錯?