Streamfield 中的 Wagtail SnippetChooserBlock [英] Wagtail SnippetChooserBlock in Streamfield

查看:25
本文介绍了Streamfield 中的 Wagtail SnippetChooserBlock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从片段中获取值时遇到了一些问题,我已使用片段选择器块将其包含到流字段中.

I am having some trouble getting the values from a snippet, that I have included into a streamfield using a Snippet Chooser Block.

生物片段:

@register_snippet
class BioSnippet(models.Model):
    name = models.CharField(max_length=200, null=True)
    job_title = models.CharField(max_length=200, null=True, blank=True)
    bio = RichTextField(blank=True)
    image = models.ForeignKey(
        'wagtailimages.Image',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+',
        verbose_name='Bio Image'
    )
    contact_email = models.CharField(max_length=50, null=True, blank=True)
    contact_phone = models.CharField(max_length=50, null=True, blank=True)

    panels = [
        FieldPanel('name'),
        FieldPanel('job_title'),
        FieldPanel('bio'),
        ImageChooserPanel('image'),
        FieldPanel('contact_email'),
        FieldPanel('contact_phone'),
    ]

    def __str__(self):
        return self.name

    class Meta:
        ordering = ['name',]

生物流场定义:

class BioInline(StructBlock):
    bio = SnippetChooserBlock(BioSnippet)

class BioBlock(StructBlock):
    overall_title = CharBlock(required=False)
    bios = ListBlock(BioInline())

这一切正常,但是当我到达模板时,我似乎无法访问代码段的值

This all works, but when I get to the template, I cannot seem to access the values of the snippet

{% for b in child.value.bios %}
    {{ b }}

    <hr>
    {{ b.name }}

{% endfor %}

{{ b }} 标签输出:

the {{ b }} tag outputs:

bio
Sales Team

然而 {{ b.name }} 什么都不输出.{{ b.values.name }} 或我能猜到的任何其他排列也没有.我怀疑这些值只是没有被拉低.

However {{ b.name }} outputs nothing. Neither does {{ b.values.name }} or any other permutation I can guess at. I suspect the values are just not being pulled down.

推荐答案

bios 这里定义为一个 BioInline 值的列表,所以 b 将是一个 BioInline 值 - 它具有单个属性 bio(为您提供实际的 BioSnippet 对象).要获得名称,您必须使用:{{ b.bio.name }}.

bios here is defined as a list of BioInline values, and so b in your template would be a BioInline value - which has a single property, bio (giving you the actual BioSnippet object). To get the name, you'd therefore have to use: {{ b.bio.name }}.

我认为 BioInline 对象实际上并没有给您带来任何好处 - 您可以将 BioBlock 定义为:

I don't think the BioInline object is actually gaining you anything though - you could instead define BioBlock as:

class BioBlock(StructBlock):
    overall_title = CharBlock(required=False)
    bios = ListBlock(SnippetChooserBlock(BioSnippet))

这将使 bios 成为 BioSnippets 的列表 - {{ b.name }} 然后将按预期工作.

which would make bios a list of BioSnippets - {{ b.name }} would then work as expected.

这篇关于Streamfield 中的 Wagtail SnippetChooserBlock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆