2013-04-29 25 views
1

我有一个包含12个类别的对象列表,我想获得这个列表的前3个,只显示最后一个条目这3个itens中的每一个的数据库。我究竟做错了什么?我不能拿只需要3个特定类别的最后一个项目...Flask/Mongo/Jinja - 切片一个ListField并显示数据库的最后一个条目

这是我app.py(不工作!):

# try and get experiences where mood_name is inside the mood list 
try: 
    experiences = models.Experience.objects(mood=mood_name) 

except: 
    abort(404) 

# get the first 3 interests inside Experience 
ints = models.Experience.objects.fields(slice__interest=[0,2]) 

interest =() 

for i in ints: 

    interest = 'i' 
    # get the last experience of a specific interest 
    exp_highlight = models.Experience.objects(interest=interest).order_by('-timestamp') 

这是我的HTML /神社(不工作):

{% for experience in experiences if experience.mood == 'Chill' %} 
      {% if loop.index <= 2 %} 

      <div class="item"> 
      <img class="image-highlight" src="https://s3.amazonaws.com/aliceapp/{{experience.filename}}"/> 

       <div id="purple-small-box"> 
       <h6>{{ experience.interest }}</h6> 
       </div> 

       <div class="small-boxes"> 
        <h3>{{ experience.title }}</h3> 
        <p>{{ experience.description }}</p> 
       </div> 
      </div> 

      {% endif %} 
     {% endfor %} 

回答

1

我认为这个问题是这一行:

interest = 'i' 

你可能要像

interest = str(i) 

甚至

interest = i 

否则,你的查找是要去寻找感兴趣的地方目标是“我”,而不是一些数字。

相关问题