Django-CMS 自定义插件和占位符
Django-CMS Custom plugin and placeholders
我是 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>
问题来了,当我创建插件并插入 text/picture/link 插件时,这些插件没有填充,所以基本上一切都是空的,就像 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
等。
编辑:
如果您有兴趣,除了您的型号信息外,instance
中还包含许多其他内容。例如,在我的一个 cms_plugins.py
文件中,我在一个名为 carousel
的模型的 CMSPlugin 中添加了 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 的新手,我查看了文档,但找不到我需要的东西,所以我可能做错了。 不管怎样,我正在创建一个自定义插件,我的模型里面有 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>
问题来了,当我创建插件并插入 text/picture/link 插件时,这些插件没有填充,所以基本上一切都是空的,就像 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
等。
编辑:
如果您有兴趣,除了您的型号信息外,instance
中还包含许多其他内容。例如,在我的一个 cms_plugins.py
文件中,我在一个名为 carousel
的模型的 CMSPlugin 中添加了 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']