Wagtail块:在覆盖的get-contex中访问上下文和请求

2024-09-29 17:14:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从context获取页面和请求,以便能够在块内使用分页。我得到的唯一背景是

context {'self': None, 'value': None}

甚至可以在streamfield块内进行分页吗?在

class CustomStaticBlock(blocks.StaticBlock):


    def get_context(self, value):
        context = super(CustomStaticBlock, self).get_context(value)

渲染

^{pr2}$

Tags: selfnonegetvaluedefcontext页面class
2条回答

外部页面的上下文在块模板中可用,但不幸的是,get_context方法中不可用。(这是由于模板上下文的构建方式,get_context的结果是merged into the parent context。)这是一个已知的限制:

https://github.com/wagtail/wagtail/pull/2786#issuecomment-230416360https://github.com/wagtail/wagtail/issues/2824

一种可能的解决方法是重写render方法(这是公认的不理想的,因为您必须在那里重复部分或全部现有的render逻辑)。在

它现在正在工作(在get_context内)。如果有人对StreamField有相同的问题,请确保将其呈现为:

{% for block in page.body %} {% include_block block %} {% endfor %}

以下操作无效(空parent_context):

{% include_block page.body %}

{{ page.body|safe }}

相关问题 更多 >

    热门问题