python - Flask/Mongo/Jinja - 切片 ListField 并显示数据库的最后条目

标签 python mongodb flask jinja2 database

我有一个对象内包含 12 个类别的列表,我想获取此列表的前 3 个 itens,并仅显示这 3 个 itens 中每一个的数据库的最后一个条目。我究竟做错了什么?我不能只取 3 个特定类别的最后一个条目...

这是我的 app.py(不工作!):<​​/p>

# 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/Jinja(不工作):

{% 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 %}   

最佳答案

我认为问题出在这一行:

interest = 'i'

你可能想要类似的东西

interest = str(i)

甚至

interest = i

否则,您的查找将查找感兴趣的是“i”而不是某个数字的对象。

关于python - Flask/Mongo/Jinja - 切片 ListField 并显示数据库的最后条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283921/

相关文章:

python - 将 .xls 转换为 .xlsx 以便 Openpyxl 可以使用它

python - Pandas GroupBy - 仅显示具有多个独特特征值的组

Python 不能修改 MySQL 但用户可以

python - 无法正确展开 numpy 数组

mongodb - 如何优雅地关闭在 docker 中运行的 monogodb 0.8

java - 使用 DropWizard 和 MongoDB 在多个值中搜索关键字

mysql - 哪个数据库适合这项工作?

flask - 我可以将 AWS Lambda 站点移动到私有(private)域吗?

python - 为什么我的 Flask 错误处理程序没有被调用?

python - 使用 Flask-Security 进行 Flask 身份验证