我有一个名为MultiColumnResponsive的定制插件,它接收列数作为参数,并且只接受子级的ColumnResponsive插件。我想创建默认情况下作为子级嵌套的ColumnResponsive插件,但我无法做到这一点。你知道吗
我现在的代码是:
厘米_插件.py
class MultiColumnResponsivePlugin(CMSPluginBase):
name = "Multi Column Responsive"
module = _("Containers")
model = MultiColumn
render_template = "plugin/multi-column-responsive/multi-column-responsive.html"
allow_children = True
child_classes = ["ColumnResponsivePlugin"]
def render(self, context, instance, placeholder):
context = super(MultiColumnResponsivePlugin, self).render(context, instance, placeholder)
return context
class ColumnResponsivePlugin(CMSPluginBase):
name = "Column Responsive"
module = _("Containers")
render_template = "plugin/column-responsive/column-responsive.html"
allow_children = True
parent_classes = ["MultiColumnResponsivePlugin"]
def render(self, context, instance, placeholder):
context = super(ColumnResponsivePlugin, self).render(context, instance, placeholder)
return context
型号.py
class MultiColumn(CMSPlugin):
NUM_OF_COLUMNS = (
(1, '1'),
(2, '2'),
)
num_of_columns = models.IntegerField(default=1, choices=NUM_OF_COLUMNS)
这是我添加一个包含两列的多列响应插件时所期望的结果:
编辑:经过几天的研究。我通过在MultiColumnResponsivePlugin类的save_model
函数中添加几行代码实现了这一点:
def save_model(self, request, obj, form, change):
response = super(MultiColumnResponsivePlugin, self).save_model(
request, obj, form, change
)
for x in range(int(form.cleaned_data['num_of_columns'])):
add_plugin(obj.placeholder, ColumnResponsivePlugin.__name__, obj.language, target=obj)
return response
你可以试试这个:
关于这一点的一些注意事项:
如果您需要一个临时值,比如
wanted_children
,请使用一个表单,不要污染您的模型!你知道吗此代码段采用了djangocms-cascade,它使用类似的方法来实现相同的结果。你知道吗
相关问题 更多 >
编程相关推荐