Django-CMS 自定义插件和占位符

标签 django python-3.x django-cms

我对 Django-CMS 真的很陌生,我查看了文档,但我找不到我需要的东西,所以我可能做错了。 不管怎样,我正在创建一个自定义插件,我的模型里面有 3 个占位符:

models.py

class Tile(CMSPlugin):
   text = PlaceholderField('text', related_name='tile_text')
   img = PlaceholderField('img', related_name='tile_img')
   link = PlaceholderField('link', related_name='tile_link')

然后我有 3 个不同的插件使用该模型:

cms_plugins.py

from cms.plugin_base import CMSPluginBase
from cms.models.pluginmodel import CMSPlugin
from cms.plugin_pool import plugin_pool
from django.utils.translation import ugettext_lazy as _

from tiles_plugin.models import Tile


class BigTilePlugin(CMSPluginBase):
    model = Tile
    name = _('Big Tile Plugin')
    render_template = 'tiles_plugin/big_tile.html'
    allow_children = True

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        return context

class MedTilePlugin(CMSPluginBase):
    model = Tile
    name = _('Medium Tile Plugin')
    render_template = 'tiles_plugin/med_tile.html'
    allow_children = True

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        return context

class SmallTilePlugin(CMSPluginBase):
    model = Tile
    name = _('Small Tile Plugin')
    render_template = 'tiles_plugin/small_tile.html'
    allow_children = True

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        return context


plugin_pool.register_plugin(BigTilePlugin)
plugin_pool.register_plugin(MedTilePlugin)
plugin_pool.register_plugin(SmallTilePlugin)

然后我就有了模板:

big_tile.html

{% load cms_tags %}
<div class="big-tile">
    <div class="content">
        {% render_placeholder instance.text %}
    </div>
    <div class="link">
        {% render_placeholder instance.link %}
    </div>
    <div class="img">
        {% render_placeholder instance.img %}
    </div>
</div>

问题来了,当我创建插件并插入文本/图片/链接插件时,这些插件没有填充,所以基本上所有内容都是空的,就像 javascript 没有填充它一样(当然它不是) javascript),但我想我错过了与 child_plugins 的关系。 谁能帮帮我吗?

谢谢

编辑

这使其正常工作:

    {% for plugin in instance.child_plugin_instances %}
        {% render_plugin plugin %}
    {% endfor %}

感谢@mfcovington

最佳答案

看起来几乎一切都是正确的。但是,例如,要获取 text,您需要使用 instance.tile.text 而不是 instance.text

因此,您的模板应如下所示:

{% load cms_tags %}
<div class="big-tile">
    <div class="content">
        {% render_placeholder instance.tile.text %}
    </div>
    <div class="link">
        {% render_placeholder instance.tile.link %}
    </div>
    <div class="img">
        {% render_placeholder instance.tile.img %}
    </div>
</div>

或者,当您设置上下文时,您可以设置context['tile']:

class BigTilePlugin(CMSPluginBase):
    ...
    def render(self, context, instance, placeholder):
        context['tile'] = instance.tile
        return context

这将允许您在模板中使用 tile.text 等,而不是 instance.tile.text 等。

编辑:

如果您感兴趣,除了模型信息之外,实例中还包含许多其他内容。例如,在我的一个 cms_plugins.py 文件中,我在 CMSPlugin 中为名为 carousel 的模型添加了 print(dir(instance)) ,并且我的终端得到以下输出(当然,它与您的不同,但大多数内容都是相同的):

['DoesNotExist', 'Meta', 'MultipleObjectsReturned', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_base_manager', '_carousel_cache', '_check_column_name_clashes', '_check_field_name_clashes', '_check_fields', '_check_id_field', '_check_index_together', '_check_local_fields', '_check_m2m_through_same_relationship', '_check_managers', '_check_model', '_check_ordering', '_check_swappable', '_check_unique_together', '_db_connection', '_default_manager', '_deferred', '_do_insert', '_do_update', '_get_FIELD_display', '_get_basepath', '_get_children_path_interval', '_get_database_connection', '_get_database_cursor', '_get_lastpos_in_path', '_get_next_or_previous_by_FIELD', '_get_next_or_previous_in_order', '_get_parent_path_from_path', '_get_path', '_get_pk_val', '_get_serializable_model', '_get_unique_checks', '_inc_path', '_inst', '_int2str', '_meta', '_perform_date_checks', '_perform_unique_checks', '_placeholder_cache', '_prepare_pos_var', '_prepare_pos_var_for_add_sibling', '_prepare_pos_var_for_move', '_process_foreign_keys', '_render_meta', '_save_parents', '_save_table', '_set_pk_val', '_state', '_str2int', '_valid_pos_for_add_sibling', '_valid_pos_for_move', '_valid_pos_for_sorted_add_sibling', '_valid_pos_for_sorted_move', 'add_child', 'add_root', 'add_sibling', 'add_url', 'alias_reference', 'aliaspluginmodel', 'alphabet', 'carousel', 'carousel_id', 'carouselplugin', 'changed_date', 'check', 'child_plugin_instances', 'clean', 'clean_fields', 'cmsplugin_ptr', 'cmsplugin_ptr_id', 'cmsplugin_set', 'column', 'copy_plugin', 'copy_relations', 'copy_url', 'creation_date', 'date_error_message', 'delete', 'delete_url', 'depth', 'dump_bulk', 'edit_url', 'file', 'find_problems', 'fix_tree', 'flash', 'full_clean', 'gap', 'get_ancestors', 'get_annotated_list', 'get_breadcrumb', 'get_breadcrumb_json', 'get_children', 'get_children_count', 'get_database_vendor', 'get_depth', 'get_descendant_count', 'get_descendants', 'get_descendants_group_count', 'get_first_child', 'get_first_root_node', 'get_first_sibling', 'get_foreign_keys', 'get_instance_icon_alt', 'get_instance_icon_src', 'get_last_child', 'get_last_root_node', 'get_last_sibling', 'get_media_path', 'get_next_by_changed_date', 'get_next_by_creation_date', 'get_next_sibling', 'get_parent', 'get_plugin_class', 'get_plugin_class_instance', 'get_plugin_instance', 'get_plugin_name', 'get_position_in_placeholder', 'get_prev_sibling', 'get_previous_by_changed_date', 'get_previous_by_creation_date', 'get_root', 'get_root_nodes', 'get_short_description', 'get_siblings', 'get_sorted_pos_queryset', 'get_translatable_content', 'get_tree', 'googlemap', 'has_change_permission', 'id', 'inheritpageplaceholder', 'is_child_of', 'is_descendant_of', 'is_leaf', 'is_root', 'is_sibling_of', 'language', 'link', 'load_bulk', 'move', 'move_url', 'multicolumns', 'node_order_by', 'notify_on_autoadd', 'notify_on_autoadd_children', 'num_children', 'numchild', 'numconv_obj', 'numconv_obj_', 'objects', 'page', 'parent', 'parent_id', 'path', 'picture', 'pk', 'placeholder', 'placeholder_id', 'placeholderreference', 'plugin_type', 'position', 'post_copy', 'prepare_database_save', 'reload', 'render_plugin', 'save', 'save_base', 'serializable_value', 'set_base_attr', 'set_translatable_content', 'shinyapppluginmodel', 'steplen', 'teaser', 'text', 'translatable_content_excluded_fields', 'unique_error_message', 'validate_unique', 'video']

关于Django-CMS 自定义插件和占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31009021/

相关文章:

python - 添加 super 用户时 Django SYNCDB 失败(Windows 7、mySQL、Django 1.2.4、mySQL 5.5)

python - settings.py 中模板文件夹的 Django 路径

python - 为什么我的代码给出了错误的变量值?

python-3.x - plotly:分组条形图中的降序

python - 小部件在弹出窗口上没有响应

python - 如何在 Django 中的每个管理页面加载上运行 python 函数

python - Django 从表单上传文件无法验证

django - 强行禁用 Django CMS 中的缓存

django - 选择起始页 django-cms

javascript - 如何处理 DjangoCMS i18n URL (/en/my_page,/fr/my_page...) 和 AngularJS?