【发布时间】:2021-10-06 19:31:19
【问题描述】:
我想按价格从低到高和从高到低对产品列表进行排序并在模板中呈现
我的模型.py
class Item(models.Model):
categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='our_items')
subcategories = models.ForeignKey(Subcategories, on_delete=models.CASCADE, related_name='products')
can_buy = models.ForeignKey(For, on_delete=models.CASCADE, related_name='for_wearing')
name = models.CharField(max_length=200, blank=False)
contain_size = models.CharField(max_length=50, blank=True)
brand_name = models.CharField(max_length=100, blank=False, default='Bagh')
first = models.ImageField(upload_to='items', blank=False)
second = models.ImageField(upload_to='items', blank=False)
third = models.ImageField(upload_to='items', blank=True)
fourth = models.ImageField(upload_to='items', blank=True)
fifth = models.ImageField(upload_to='items', blank=True)
rating = models.FloatField(blank=False,default='4.0')
item_video = models.FileField(upload_to='item_vedio', blank=True)
color = models.CharField(max_length=30, blank=False, default='Black')
material = models.CharField(max_length=50, blank=False, default='Plastic' )
return_policy = models.CharField(max_length=100, blank=False, default='7Days Return Policy')
stock = models.CharField(max_length=50, blank=False, default='In Stock')
price = models.FloatField(blank=False,)
actual_price = models.FloatField(blank=False)
type = models.CharField(blank=False, max_length=100, default='washable')
about = models.CharField(blank=False,max_length=100, default='Lusturous')
offer = models.CharField(max_length=4, blank=True)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
update_at = models.DateTimeField(auto_now=True)
description = models.TextField(blank=True)
def __str__(self):
return self.name
class Subcategories(models.Model):
categories = models.ForeignKey(Categories, on_delete=models.CASCADE, related_name='our_categories')
name = models.CharField(max_length=200, blank=False)
joined_date = models.DateTimeField(default=timezone.now,editable=False)
update_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
这是我的意见.py
class Product(View):
def get(self, request, subcategory_id):
subcategory = get_object_or_404(Subcategories, pk=subcategory_id)
products = subcategory.products.all()
category_list = Categories.objects.all()
print(products)
return render (request, 'products.html',{"subcategory_list" : products, 'category_list': category_list })
我的html
<div class="col-10">
<ul>
<li class="filters"><strong>Sort By :</strong></li>
<li class="filters"><a class="filter_by" href="">Popularity</a></li>
<li class="filters"><a class="filter_by" href="">Price:--low to high</a></li>
<li class="filters"><a class="filter_by" href="">Price:-- high to low</a></li>
<li class="filters"><a class="filter_by" href="">Newest First</a></li>
<li class="filters"><a class="filter_by" href="">Discount</a></li>
</ul>
</div>
<section class="col-10">
{% for products in subcategory_list %}
<a class="products" href="{% url 'products:detail' item_id=products.id %}">
<div class="card col-4" style="width: 18rem; height:420px">
<img class="img" src="{{ products.first.url }}" alt="" height="300px" width="100%" onmouseover="this.src='{{ products.second.url }}'"
onmouseout="this.src='{{ products.first.url }}'" >
<div class="detail">
<h4 class="price">{{ products.price|currency}}</h4>
<h5>{{products.name}} <br> <span class="brand">{{ products.brand_name}}</span> <span class="btn btn-success buton">✔Authentic</span> </h5>
<p class="rate">Rate: {{products.rating}}<i class="fas fa-star"></i></p>
<h6>Size:<span> {{products.contain_size}}</span> </h6>
</div>
</div>
</a>
{% endfor %}
</div>
</section>
如您所见,我创建了一个 div 并在其中进行了排序,因此当用户单击该链接时,它会按该顺序呈现产品列表,但是我不知道如何看到一些文档,但是这似乎不是实现这一目标的最佳方式任何想法是实现这一目标的最佳方式 谢谢你的时间
【问题讨论】:
-
subcategory.products.all()是什么?请使用子类别模型更新您的问题,然后我可以告诉您要使用的确切查询。 -
@Prakhar 添加了子类别
-
subcategory.products实际提供的是什么,那里没有产品字段! -
products 是外键中的相关名称,带有链接的子类别
-
啊!明白了我的错:D
标签: django django-models django-views django-templates django-class-based-views