0

I make a query to my database and select the collection reviews. I want to filter, to organize, to group, to sort and to limit my results from the query.

The query is in views.py. It works fine but i have a problem to print the content of context variable "best_hotels" in the html file.

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()

Here is the 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 )

Her is the 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>
...

Here the results of 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

The name of the hotel is missing. Why?

I changed the html file to check if the name of the hotels exist. Here is the new one:

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

Here is the result of the new html file

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

Here you can see that the hotelnames are there. But why not in the first html file?

1 Answers1

0

I dont understand where you printing name in the first html file. There is no element as {{ row.name }} inside the li tag.

Aayushi
  • 73
  • 1
  • 8