我是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插件%} {%endfor%}
感谢@mfcovington
答案 0 :(得分:1)
看起来几乎一切都是正确的。但是,要获得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
文件中,我在一个名为print(dir(instance))
的模型的CMSPlugin中添加了carousel
,并将以下输出发送到我的终端(它当然与您的终端不同,但大多数事情都是共同的):
['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']