【问题标题】:Rendering streamfield block a certain way if booleanBlock is True如果 booleanBlock 为 True,则以某种方式渲染流场块
【发布时间】:2017-05-24 14:51:02
【问题描述】:

您好,我目前是 django/wagtail 的新手。我正在处理一个显示以前和当前工作/职位的关于页面。由于经验量不受限制,因此我已将位置流场阻塞。这是我的模型的代码。

#Create experience block 
class ExperienceBlockStruct(StructBlock):
    position_title = CharBlock(help_text="Enter a previous position title.")
    description = CharBlock(help_text="Job description")
    current_position = BooleanBlock(required=False, help_text="Check if 
    current position")

class Meta:
    template = 'blocks/experience_block.html'


class ExperienceBlock(StreamBlock):
    experience = ExperienceBlockStruct(icon="form")

这是我使用模型的页面

class About(Page):
    profile_pic = "some image reduced code bc it currently works"
    bio = RichTextField(blank=True)
    resume = "some document reduced code bc it currently works"
    experience = StreamField(ExperienceBlock())
    content_panels = Page.content_panels + [
        ImageChooserPanel('profile_pic'),
        FieldPanel('bio'),
        DocumentChooserPanel('resume'),
        StreamFieldPanel('experience'),
    ]

现在我遇到的问题是如何将current_position = True 的块与那些不在的区域呈现在不同的区域中。 我试过了

templates/about.html
{% for block in page.experience %}
  {% if block.current_position %}
    {% include_block block %}
  {% endif %}
{% endfor %}

但这并没有渲染任何东西。我也试过了

<div class="experience">
  {% if value.current_position %}
    {{ value.position_title }}
  {% else %}
    {{ value.position_title }}
  {% endif %}
</div>

但这会为每个块创建一个新的 div。我想要实现的是blocks/experience_block.html

<div>
  Current position(s): {% blocks with current_postion == True %}
</div>   

<div>
  Past position(s): {% blocks with current_postion == False %}
</div>  

我怎样才能实现这样的目标?

【问题讨论】:

    标签: python django jinja2 wagtail wagtail-streamfield


    【解决方案1】:

    您的第一个模板 sn-p 几乎是正确的 - 您只需检查 block.value.current_position 而不是 block.current_position:

    {% for block in page.experience %}
        {% if block.value.current_position %}
            {% include_block block %}
        {% endif %}
    {% endfor %}
    

    这是因为循环遍历page.experience 会为您提供一系列BoundBlock 对象,这些对象会告诉您block_type(在您的情况下始终为'experience')以及块值。更详细的解释见BoundBlocks and values。

    您可以在您的 experience_block.html 模板中执行相同的操作(使用 {% for block in value %} 而不是 {% for block in page.experience %}) - 尽管请注意 Meta 模板定义需要继续 ExperienceBlock 而不是 ExperienceBlockStruct,因为那是可以访问要循环的完整列表的列表,而不是单个记录。

    为了使事情更整洁,我建议在块上定义一个get_context 方法,这样您就可以在 Python 代码中而不是在模板中进行数据操作...

    class ExperienceBlock(StreamBlock):
        experience = ExperienceBlockStruct(icon="form")
    
        def get_context(self, value, parent_context=None):
            context = super(ExperienceBlock, self).get_context(value, parent_context=parent_context)
            context['current_positions'] = [block for block in value if block.value.current_position]
            context['past_positions'] = [block for block in value if not block.value.current_position]
            return context
    
        class Meta:
            template = 'blocks/experience_block.html'
    

    这将使变量current_positions 和past_positions 在模板上可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 2021-08-16
      相关资源
      最近更新 更多