【问题标题】:List of Articles form FeinCMS Content TypsFein CMS 内容类型的文章列表
【发布时间】:2015-10-16 18:20:58
【问题描述】:

我的任务是获取文章列表。本文来自一个简单的 FeinCMS ContentType。

class Article(models.Model):
       image = models.ForeignKey(MediaFile, blank=True, null=True, help_text=_('Image'), related_name='+',)
       content = models.TextField(blank=True, help_text=_('HTML Content'))
       style = models.CharField(
                    _('template'),max_length=10, choices=(
                            ('default', _('col-sm-7 Image left and col-sm-5 Content ')),
                            ('fiftyfifty', _('50 Image left and 50 Content ')),
                            ('around', _('small Image left and Content around')),
                            ),
                            default='default')
        class Meta:
                abstract = True
                verbose_name = u'Article'
                verbose_name_plural = u'Articles'

        def render(self, **kwargs):
                return render_to_string('content/articles/%s.html' % self.style,{'content': self,})

我想在不同的子页面中使用它。

现在在主页上获得所有文章的列表会很棒(我的项目 -> project1、project2、project3 的列表)。

类似:Article.objects.all() 模板:

{% for entry in article %}
    {% if content.parent_id == entry.parent_id %} #only projects
        <p>{{ entry.content|truncatechars:180 }}</p>
    {% endif %}
{% endfor %}  

但我得到一个错误“类型对象'Articles'没有属性'对象'...... 你有一个聪明的主意吗?使用 Feincms ContentType 会很不错。

【问题讨论】:

    标签: django templates feincms


    【解决方案1】:

    FeinCMS 内容类型为abstract,这意味着没有数据和与之关联的数据库表。所以没有objects经理,也没有办法查询。

    在做Page.create_content_type() 时,FeinCMS 采用内容类型和对应的Page 类,并创建一个包含实际数据的(非抽象)模型。为了访问新的具体模型,您需要使用content_type_for。换句话说,您正在寻找:

    from feincms.module.page.models import Page
    PageArticle = Page.content_type_for(Article)
    articles = PageArticle.objects.all()
    

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 2012-11-22
      • 2017-06-21
      • 1970-01-01
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多