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>]>
我在做什么错?