3

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())

これはすべて機能しますが、テンプレートに到達すると、スニペットの値にアクセスできないようです

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

    <hr>
    {{ b.name }}

{% endfor %}

{{ b }} タグの出力:

bio
Sales Team

ただし、 {{ b.name }} は何も出力しません。{{ b.values.name }} または私が推測できる他の順列もありません。値が引き下げられていないだけだと思います。

4

2 に答える 2

0

または、self.bios を使用できます

blocks.py では、スニペット モデルをインポートする必要があります (これは既にあるはずです)。

from thebioapp.models import BioSnippet

そして、このモデルをテンプレート自体で使用します

{% for b in self.bios %}
    {{ b }}

    <hr>
    {{ b.name }}

{% endfor %}

投稿は古いですが、ワグテールの人気が高まっているので、これが他の人に役立つことを願っています!

于 2020-03-26T13:58:16.550 に答える