【问题标题】:How do I add a plugin to a Static placeholder in Django CMS如何将插件添加到 Django CMS 中的静态占位符
【发布时间】:2014-08-24 17:48:42
【问题描述】:

我开始在 Django-CMS 中使用静态占位符,我想加快步伐并创建一个“页脚”静态占位符。比如:

static_placeholder = StaticPlaceholder(
    name=static_placeholder_code,
    code=static_placeholder_code,
    creation_method=StaticPlaceholder.CREATION_BY_CODE
)
static_placeholder.save()

我尝试使用 api.add_plugin 添加 TextPlugin 但出现错误

add_plugin(
    placeholder=static_placeholder,
    plugin_type='TextPlugin',
    language='en',
)

由于 static_placeholder 不是 Placeholder 的实例,所以 add_plugin 不起作用。 在 add_plugin 函数中:assert isinstance(placeholder, Placeholder)

将 TextPlugin 添加到此静态占位符的最佳方法是什么?

【问题讨论】:

    标签: python django django-cms


    【解决方案1】:

    StaticPlaceholder 是一个与Placeholder 模型有两个外键关系的模型,一个叫做draft,另一个叫做public。两者都会给你一个Placeholder 实例。

    你可以使用:

    add_plugin(
        placeholder=static_placeholder.draft,
        plugin_type='TextPlugin',
        language='en',
    )
    

    它会起作用,但请记住,您应该始终使用draft 作为上面的示例,这是因为当您发布时,来自draft 的所有插件都将被复制到public 占位符。

    最后不确定您的应用程序的细节,但应该指出,当静态占位符在模板中呈现时,没有必要事先创建它。你可以这样做:

    {% load cms_tags %}
    
    {% static_placeholder 'footer' %}
    

    这将获取或创建 footer 静态占位符。

    【讨论】:

    • 我通过代码创建了占位符,但感谢您的澄清!为解释发布过程多点赞,可能为我节省了一大堆时间!
    猜你喜欢
    • 2015-01-18
    • 2015-06-21
    • 2014-12-10
    • 2012-10-10
    • 2016-05-04
    • 2015-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多