Reputation: 29
How do you query the database when you select highest, it orders product from the most expensive price to list and vice versa. Here is my templates. I think you can see the select sort.
<section class="body-section">
<!--Cleaning services-->
{%for category in categories%}
<section class="main-products cooler">
<div class="upper-bar">
<div>
<h2>{{category.name}}</h2>
</div>
<div class="sortby">
<span>Sort By: <select class="sort">
<option value="highest">Highest Price</option>
<option value="lowest">Lowest Price</option>
</select></span>
</div>
</div>
<hr />
<!--Single Products-wrap-->
<div class="specific-product-wrap specific-product-wrap-cooler">
{% for product in category.product_set.all %}
<a href="{%url 'product' product.pk%}">
<div class="specific-single-product">
<div class="product-image center">
<img class="product-image-shape" src="{{product.image.url}}" alt="adamol Distilled" />
</div>
<div class="produc-descriptions">
<h4 class="specific-product-title">{{product.title}}</h4>
<p class="one-line-description"></p>
<p class="price">Ksh.{{product.price}}</p></a>
<button data-product="{{product.id}}" data-action="add" class="AddToCart update-cart" id="addToCart update-cart">Add To
Cart</button>
</div>
</div>`
{% empty %}
<p>No Product Lol</p>
{%endfor%}
</div>
</section>
Here is my model.py
class Product(models.Model):
image = models.ImageField(null=False, blank=False)
title = models.CharField(max_length=2000, null=False, blank=False)
category = models.ForeignKey(
Category, on_delete=models.CASCADE, default=True, null=False)
price = models.DecimalField(max_digits=5, decimal_places=2)
description = models.TextField()
delivery_time = models.DateTimeField(default=datetime.now, blank=True)
is_published = models.BooleanField(default=True)
created_at = models.DateTimeField(default=datetime.now, blank=True)
digital = models.BooleanField(default=False, null=True, blank=False)
def __str__(self):
return self.title
I want the user to be able to view product, when they select highest, it sorts, when they select lowest it sorts by prices. Have truid different methods they all mess up with my program. Here is my views.py
def waterProducts(request):
categories = categories = Category.objects.filter(
name__startswith="Products").order_by('name')
page_num = request.GET.get("page")
paginator = Paginator(categories, 3)
try:
categories = paginator.page(page_num)
except PageNotAnInteger:
categories = paginator.page(1)
except EmptyPage:
categories = paginator.page(paginator.num_pages)
if request.user.is_authenticated:
customer = request.user
order, created = Order.objects.get_or_create(
customer=customer, complete=False)
items = order.orderitem_set.all()
cartitems = order.get_cart_items
else:
items = []
order = {' get_cart_total': 0, 'get_cart_items': 0}
cartitems = order['get_cart_items']
waters = Product.objects.all()
context = {
'categories': categories, 'waters': waters, 'cartitems': cartitems
}
return render(request, "products/WaterProductPage.html", context)
Upvotes: 0
Views: 303
Reputation: 2442
This is how you can order your products by Highest price :
ordered_product = Product.objects.order_by('-price')
Notice the - before price
If you want show the result according to the template select value :
views.py
def waterProducts(request):
# retrieve the order_by variable from url
order_by = request.GET.get('order_by', None)
if order_by == 'highest':
# the variable is set in the url
products = Product.objects.order_by('-price')
else:
# The variable is not set
products = Product.objects.order_by('price')
template.html
<div class="sortby">
<span>Sort By:
<select class="sort">
<option value="highest"><a href="{% url 'product_page_url' %}?order_by=highest">Highest Price</a></option>
<option value="lowest"><a href="{% url 'product_page_url' %}?order_by=lowest">Lowest Price</a></option>
</select>
</span>
</div>
Upvotes: 1