0

我将 cmsplugin-blog 与 Django-cms 一起使用。我想在主页中显示最新博客条目的列表(不与插件挂钩)。为此,我创建了一个模板标签。我可以获得一组条目,但不知道如何呈现它们的标题。谁能指出我正确的方向?

这是代码。

cmsplugin_blog_tags.py:

17 # blog post widget
18 @register.inclusion_tag('cmsplugin_blog/entry_list_1_snippet.html', takes_context=True)
19 def render_entries_widget(context, entry_num):
20     request = context["request"]
21     language = get_language_from_request(request)
22     kw = get_translation_filter_language(Entry, language)
23     
24     return {
25         'entries': Entry.published.filter(**kw)[0:entry_num],
26     }

entry_list_1_snippet.html:

 1 {% load i18n placeholder_tags cmsplugin_blog_tags simple_translation_tags %}
 2 
 ...
 3 <div id="featposts-widget-2" class="widget featposts">
 4   <div class="widget-wrap">
 5     <h3 class="widgettitle"><span><a href="/blog">{% trans "VFOSS Blog" %}</a></span></h3>
 6     <div class="cat-posts-widget textwidget">
 7 
 8       {% for entry in entries|annotate_with_translations %}
           # line 9 does not work. Probably no request.
 9         {% with entry|get_preferred_translation_from_request:request as title %}
           # no excerpt showed.
10         {% with entry.placeholders|choose_placeholder:"blog_excerpt" as excerpt %}
11 
4

1 回答 1

1

I achieved this by injecting a list into the context with templatetag and included other html for rendering the list.

the tag:

 # blog post widget
 20 @register.tag
 21 def get_latest_entries(parser, token):
 22   bits = token.contents.split()
 23   if len(bits) != 4:
 24     raise TemplateSyntaxError, "(cmsplugin_blog) get_latest_entries tag takes exactly 3 arguments)"
 25   if bits[2] != 'as':
 26     raise TemplateSyntaxError, "second argument to the get_latest_links tag must be 'as'"
 27   return LatestEntriesNode(bits[1], bits[3])
 28 
 29 class LatestEntriesNode(Node):
 30   def __init__(self, num, varname):
 31     self.num, self.varname = num, varname
 32 
 33   def render(self, context):
 34     request = context["request"]
 35     language = get_language_from_request(request)
 36     kw = get_translation_filter_language(Entry, language)
 37     context[self.varname] = Entry.published.filter(**kw)[:self.num]
 38     return ''

Placing the tag where the list should appear:

  14 {% block entries_widget %}
  15   {% get_latest_entries 3 as blog_entries %}
  16   {% if blog_entries %}
  17     {% include "cmsplugin_blog/widget_latest_entry_include.html" %}
  18   {% else %}
  19     <p>{% trans "No entries" %}</p>
  20   {% endif %}
  21 {% endblock %}
于 2013-03-08T13:03:06.123 回答