【问题标题】:Django - Mongoengine Group By + Where + Sort + Limit and print the resultsDjango - Mongoengine Group By + Where + Sort + Limit 并打印结果
【发布时间】:2017-02-13 17:37:21
【问题描述】:

我查询我的数据库并选择收藏评论。我想过滤、组织、分组、排序和限制我的查询结果。

查询位于views.py 中。它工作正常,但我在 html 文件中打印上下文变量“best_hotels”的内容时遇到问题。

from __future__ import unicode_literals
from django.db import models
from mongoengine import *

class Reviews(Document):
    # _id = IntField(primary_key=True)
    content_lenght = IntField()
    title_score = IntField()
    content_eval = StringField()
    review_stars = FloatField()
    hotel_name = StringField()
    review_score = IntField()
    city = StringField()
    helpful_reader = IntField()
    title = StringField()
    content_score = IntField()
    stars_eval = StringField()
    content = StringField()
    title_eval = StringField()
    review_eval = StringField()

这里是views.py

from el_pagination.decorators import page_templates
from django.shortcuts import render
from django.http import HttpResponse
from models import Reviews, Evaluation

def reviews(request):

    best_hotels = Reviews.objects.aggregate(
        {
            "$match": {"review_eval": "positiv"}
        },
        {
            "$group" : {"_id" : "$hotel_name", "sum" : { "$sum" : 1 } }
        },
        {
            "$sort" : {"sum" : -1}
        },
        {
            "$limit": 10
        }
    )
    context = {
    'best_hotels' : best_hotels
    }
    return render(request, 'review/reviews.html', context )

她是reviews.html

...
    <h2>The 10 best hotels with the most positiv ratings</h2>
    <ul>
    {% for row in best_hotels %}
      <li>Name: {{ row.id }} - {{ row.sum }} positiv ratings</li>
    {% endfor %}
    </ul>
...

这里是html的结果

The 10 best hotels with the most positiv ratings

Name: - 678 positiv Ratings
Name: - 387 positiv Ratings
Name: - 364 positiv Ratings
Name: - 305 positiv Ratings
Name: - 292 positiv Ratings
Name: - 269 positiv Ratings
Name: - 267 positiv Ratings
Name: - 224 positiv Ratings
Name: - 219 positiv Ratings
Name: - 181 positiv Ratings

缺少酒店名称。为什么?

我更改了 html 文件以检查酒店名称是否存在。这是新的:

<h2>The 10 best hotels with the most positiv ratings</h2>
<ul>
{% for row in best_hotels %}
  <li>{{ row}}</li>
{% endfor %}
</ul>

这是新建 html 文件的结果

{u'sum': 678, u'_id': u' Steigenberger Airport Hotel '}
{u'sum': 387, u'_id': u' Steigenberger Frankfurter Hof '}
{u'sum': 364, u'_id': u' Sheraton Frankfurt Airport Hotel & Conference Center '}
{u'sum': 305, u'_id': u' Wyndham Grand Frankfurt '}
{u'sum': 292, u'_id': u' Innside by Meli\xe1 Frankfurt Niederrad '}
{u'sum': 269, u'_id': u' Innside by Meli\xe1 Frankfurt Eurotheum '}
{u'sum': 267, u'_id': u' Hilton Garden Inn Frankfurt Airport '}
{u'sum': 224, u'_id': u" 25hours Hotel by Levi's "}
{u'sum': 219, u'_id': u' 25hours Hotel The Goldman '}
{u'sum': 181, u'_id': u' The Westin Grand Frankfurt '}

您可以在此处看到酒店名称。但是为什么不在第一个 html 文件中呢?

【问题讨论】:

    标签: django python-2.7 django-models mongoengine


    【解决方案1】:

    我不明白您在第一个 html 文件中打印名称的位置。 li 标签内没有 {{ row.name }} 元素。

    【讨论】:

    • 酒店名称在 {{ row.id }} 变量中
    • 可能是因为_id,而不是id。
    • 变量和属性不能以下划线开头。我不能使用 {{ row._id }} 或 {{ _id }}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 2019-08-28
    相关资源
    最近更新 更多