I need to bind to each alias its rating, which would then display in the template, and order products by decreasing. How can I do that?
class Items(models.Model):
name = models.CharField(max_length=15, verbose_name='Название товара', default='')
price = models.IntegerField(default=0, verbose_name='Цена')
# image = models.CharField(max_length=255, verbose_name='Картинка', default='')
image = models.ImageField(null=True, blank=True, upload_to='image/', verbose_name='Изображение')
alias = models.SlugField(verbose_name='Alias товара', default='')
rating = models.FloatField(default=0, verbose_name='Рейтинг')
def popular(request):
title = 'Популярные'
products = Items.objects.all()
category = Category.objects.all()
context = {
'products': products,
'category': category,
'title': title,
}
return render(request, 'popular/main.html', context)
You can fetch the result set from the database by ordering it the way you want:
products = Items.objects.order_by('-rating')
And in the template:
{% for product in products %}
{{product.alias}} - {{product.rating}}
{% endfor %}