Django-cms "summary view" combining content from multiple pages

Django == 1.5.1 Django-CMS == 2.4.1

I would like to do something like a summary view from all the child pages of the selected page in django-cms, pull out the title, truncated content, etc. with a more ... link for each listed child. I managed to get the titles and the paths are just fine, but I'm struggling to get the content from the placeholders.

I have a templatetag like this:

from cms.models import Page from cms.utils.page_resolver import get_page_from_path from django import template register = template.Library() @register.inclusion_tag('news_summary_item.html') def get_news_items(): news_root = get_page_from_path('news') newsitems = news_root.children.filter(published=True) return {'newsitems':newsitems} 

and here is the template that it uses:

 {% load cms_tags menu_tags %} <ul> {% for item in newsitems %} <li><a href="/{{ item.get_path }}">{{ item.get_title }}</a> {% for placeholder in item.placeholders.all %} # {% show_placeholder placeholder.slot item current_language %} # {% endfor %} </li> {% endfor %} </ul> 

Can anyone help with getting the placeholder content here? Ideally, id would like to pass it through truncatewords_html to just get a summary, but open up other ways to get the same effect.

Thanks for the tips / pointers!

+6
source share
1 answer

I had to index the CMS content in one project, and I get the contents of each placeholder, and the contents of the placeholder are stored in plug-ins attached to it

How to get the contents of a CMSPlugin in a view?

 from cms.models import CMSPlugin plugin = CMSPlugin.objects.filter(plugin_type='TextPlugin')[0] # Get first text plugin # This return the body/content of the plugin: plugin_content = plugin.get_plugin_instance()[0].body 

If you want to manage other plugins, such as PicturePlugin , you can get the text "alt", for example:

 plugin_picture_content = plugin.get_plugin_instance()[0].alt 

How to get CMSPlugin content in a template?

 # plugin_object containing a CMSPlugin {{plugin_object.get_plugin_instance.0.body}} 

I believe that if you want to get content, we are talking about TextPlugin , you need to be careful, because only the TextPlugin plugin TextPlugin has the body attribute, PicturePlugin has the alt attribute and LinkPlugin has the href attribute, etc.

Solution tailored to your problem.

You run a cycle on placeholders , so you need to get all the plugins for each placeholder and get the contents of each plugin, because the contents of the placeholders, as I mentioned earlier, are stored in the plugins attached to it (TextPlugin, PicturePlugin, LinkPlugin ...).

 ... ... ... {% for placeholder in item.placeholders.all %} # Loop over placeholders {% for plugin in placeholder.get_plugin_list %} # Get plugins for each placeholder {{plugin.get_plugin_instance.0.body|striptags}} {% endfor %} {% endfor %} ... ... ... 

And so that you only display TextPlugin content, and not other plugins that you could do:

 ... ... ... {% for placeholder in item.placeholders.all %} # Loop over placeholders {% for plugin in placeholder.get_plugin_list %} # Get plugins for each placeholder {% if 'TextPlugin' in plugin.plugin_type %} {{plugin.get_plugin_instance.0.body|striptags}} {% endif %} {% endfor %} {% endfor %} ... ... ... 
0
source

Source: https://habr.com/ru/post/944615/


All Articles