Django发布JavaScript中的循环项目

时间:2019-07-14 09:08:04

标签: javascript django for-loop django-views

我有一个电话旁,我想显示最后的帖子标题和一些单词 但是,我失败了

您对我有什么建议

views.py

post_list=Post.objects.filter(status=2).order_by('-created_date')

context={
    'posts':post_list
}
return render(request,'base.html',context)

js

new lineside({
  startOffset: 100,
  lineslideItems:[
      {% for post in posts %}
              {
      title: '<a href="{{ post.get_absolute_url }}" class="link">{{ post.title }}: </a>',
      content: '{{ post.content|truncatewords_html:10 }}',
      separator: '<span class="separator"><span class="separator-bar">/</span><span class="separator-bar">/</span></span>'
    },
      {% endfor %}
  ]
})

原始lineslideItems:

{
  title: '<a href="link" class="link">Title: </a>',
  content: 'content',
  separator: '<span class="separator"><span class="separator-bar">/</span><span class="separator-bar">/</span></span>'
},
    {
  title: '<a href="link" class="link">Title: </a>',
  content: 'content',
  separator: '<span class="separator"><span class="separator-bar">/</span><span class="separator-bar">/</span></span>'
},
{
  title: '<a href="link" class="link">Title: </a>',
  content: 'content',
  separator: '<span class="separator"><span class="separator-bar">/</span><span class="separator-bar">/</span></span>'
}

1 个答案:

答案 0 :(得分:0)

问题解决了 由于缓存问题,它无法正常工作。此外,不仅缓存,而且我添加了非逗号的空行幻灯片项

lineslideItems:[
      {% for post in posts %}
              {
      title: '<a href="{{ post.get_absolute_url }}" class="link">{{ post.title }}: </a>',
      content: '{{ post.content|truncatewords_html:10 }}',
      separator: '<span class="separator"><span class="separator-bar">/</span><span class="separator-bar">/</span></span>'
    },
      {% endfor %}
    {
      title: '<a href="link" class="link"></a>',
      content: '',
      separator: '<span class="separator"><span class="separator-bar">/</span><span 
      class="separator-bar">/</span></span>'
    }
  ]