Wagtail - 将参数传递给结构块
Wagtail - pass parameters to struct block
我正在尝试将参数传递给结构块,该结构块也有子结构块以通过选择块动态加载选项。用 init 方法尝试了这个概念,但还没有成功。下面是我的实现代码-
class DefaultThemeTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(DefaultThemeTemplate, self).__init__(**kwargs)
class Meta:
label = _('Default Theme')
class ThemeOneTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(ThemeOneTemplate, self).__init__(**kwargs)
class Meta:
label = _('Theme One')
class ThemeTwoTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(ThemeTwoTemplate, self).__init__(**kwargs)
class Meta:
label = _('Theme Two')
class Templates(blocks.StructBlock):
default_theme = DefaultThemeTemplate(folder='', required=False)
theme_one = ThemeOneTemplate(folder='', required=False)
theme_two = ThemeTwoTemplate(folder='', required=False)
def __init__(self, folder=None, **kwargs):
self.default_theme = DefaultThemeTemplate(folder=folder, required=False)
self.theme_one = ThemeOneTemplate(folder=folder, required=False)
self.theme_two = ThemeTwoTemplate(folder=folder, required=False)
super(Templates, self).__init__(**kwargs)
class Meta:
label = _('Template')
class StaticPage(Page):
template = StreamField([
('template', Templates(required=False, folder='pages'))
], null=True, blank=True, verbose_name=_('Template'))
这是带有空白选择字段的屏幕截图 -
请帮我找出我做错了什么。提前致谢。
如果您在 __init__
方法中修改子块,您需要先调用 super() ,然后在 self.child_blocks
字典中设置条目。设置 self.template
将不起作用,因为在 class 定义时,处理所有命名属性并将它们转换为子块列表的代码已经 运行。
此外,您可能需要在您创建的区块上调用 set_name('template')
。所以,像这样:
def __init__(self, folder=None, **kwargs):
super().__init__(**kwargs)
template_block = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'), required=False)
template_block.set_name('template')
self.child_blocks['template'] = template_block
我正在尝试将参数传递给结构块,该结构块也有子结构块以通过选择块动态加载选项。用 init 方法尝试了这个概念,但还没有成功。下面是我的实现代码-
class DefaultThemeTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(DefaultThemeTemplate, self).__init__(**kwargs)
class Meta:
label = _('Default Theme')
class ThemeOneTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(ThemeOneTemplate, self).__init__(**kwargs)
class Meta:
label = _('Theme One')
class ThemeTwoTemplate(blocks.StructBlock):
template = blocks.ChoiceBlock(choices=[], label=_('Select a template'), required=False)
def __init__(self, folder=None, **kwargs):
self.template = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'),
required=False)
super(ThemeTwoTemplate, self).__init__(**kwargs)
class Meta:
label = _('Theme Two')
class Templates(blocks.StructBlock):
default_theme = DefaultThemeTemplate(folder='', required=False)
theme_one = ThemeOneTemplate(folder='', required=False)
theme_two = ThemeTwoTemplate(folder='', required=False)
def __init__(self, folder=None, **kwargs):
self.default_theme = DefaultThemeTemplate(folder=folder, required=False)
self.theme_one = ThemeOneTemplate(folder=folder, required=False)
self.theme_two = ThemeTwoTemplate(folder=folder, required=False)
super(Templates, self).__init__(**kwargs)
class Meta:
label = _('Template')
class StaticPage(Page):
template = StreamField([
('template', Templates(required=False, folder='pages'))
], null=True, blank=True, verbose_name=_('Template'))
这是带有空白选择字段的屏幕截图 -
请帮我找出我做错了什么。提前致谢。
如果您在 __init__
方法中修改子块,您需要先调用 super() ,然后在 self.child_blocks
字典中设置条目。设置 self.template
将不起作用,因为在 class 定义时,处理所有命名属性并将它们转换为子块列表的代码已经 运行。
此外,您可能需要在您创建的区块上调用 set_name('template')
。所以,像这样:
def __init__(self, folder=None, **kwargs):
super().__init__(**kwargs)
template_block = blocks.ChoiceBlock(choices=get_designs(folder), label=_('Select a template'), required=False)
template_block.set_name('template')
self.child_blocks['template'] = template_block