Here, In this project I'm building a Ecommerce website and I'm using Django. So, here I want to show that if there is no product of category Electric, "Sorry, No Product is Available Right Now !!!" will be shown. where n is the number of product which is sent to this template from app views. But I'm not getting "Sorry, No Product is Available Right Now !!!" as I've no product of Electric category in my database. How to fix this? where I'm doing wrong?
views.py
def electric(request):
product = Product.objects.all()
n = len(product)
params = {'product': product, 'range':range(1,n), 'n':n}
return render(request,'electric.html',params)
electric.html
{% if n is 0 %}
<div >
<h1><b>Sorry, No Product is Available Right Now !!! </b></h1>
</div>
{% else %}
<div >
<div >
<div >
{% for i in product %}
{% if i.category == "Electric" %}
<div >
<div >
<img src="{{i.image}}" />
<div >
<h4 >{{ i.product_name}}</h4>
<h6 >
Category: {{i.category}}
</h6>
<p >{{i.description}}</p>
<div >
<div >
<h5 >Price: {{i.price}} BDT</h5>
</div>
<a href="#" ><i ></i> Add to Cart</a>
</div>
</div>
</div>
</div>
{% endif %}
{% endfor %}
</div>
</div>
</div>
{% endif %}
CodePudding user response:
views.py
def electric(request):
product = Product.objects.all()
return render(request,'electric.html',{'product': product})
electric.html
{% if product.count > 0 %}
-- your code --
{% else %}
<div >
<h1><b>Sorry, No Product is Available Right Now !!! </b></h1>
</div>
{% endif %}
Update your code as above. It will work.
