When I open the home page the paginator is not displaying. And I couldn't fix this issue.
I tried these codes, and I couldn't fix this issue. Please help me to fix this issue
Codes on views.py
from django.core.paginator import Paginator, EmptyPage, InvalidPage
def home(request, c_slug=None):
c_page = None
prodt = None
if c_slug != None:
c_page = get_object_or_404(Categ, slug=c_slug)
prodt = Products.objects.filter(category=c_page, available=True)
else:
prodt = Products.objects.all().filter(available=True)
cat = Categ.objects.all()
paginator = Paginator(prodt, 4)
try:
page = int(request.GET.get('page', '1'))
except:
page = 1
try:
products = paginator.page(page)
except (EmptyPage, InvalidPage):
products = paginator.page(paginator.num_pages)
return render(request, 'index.html', {'ct': cat, 'pr': products})
Codes on Index.html
<div class="row flex px-xl-5">
{% for i in pr.object_list %}
<div class="col-lg-3 col-md-4 col-sm-6">
<div class="product-item bg-light mb-4">
<div class="product-img position-relative overflow-hidden">
<a href="{{i.get_url}}"><img class="img-fluid w-100" src="{{i.img.url}}" alt="" style="height:250px; width:500px;">
</a>
</div>
<div class="text-center py-4">
<a class="h6 text-decoration-none text-truncate" href="{{i.get_url}}">{{i.name}}</a>
<p>{{i.des|truncatechars:80}}</p>
<div class="d-flex align-items-center justify-content-center mt-2">
<h5>RS {{i.price}}</h5><h6 class="text-muted ml-2"><del>RS 123.00</del></h6>
</div>
<p>Stocks left: {{i.stock}}</p>
<div class="d-flex align-items-center justify-content-center mb-1">
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small class="fa fa-star text-primary mr-1"></small>
<small>(99)</small>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
<div class="mx-auto">
<div class="text-center">
{% for pg in products.paginator.page_range %}
<a href="?page={{pg}}" class="btn btn-light btn-sm {% if products.number == pg %} activate {% endif %}">{{pg}}</a>
{% endfor %}
</div>
</div>
</div>
When I open the home page the paginator is not displaying. And I couldn't fix this issue.
I tried these codes, and I couldn't fix this issue. Please help me to fix this issue
<div class="mx-auto">
<div class="text-center">
{% for pg in products.paginator.page_range %}
<a href="?page={{pg}}" class="btn btn-light btn-sm {% if products.number == pg %} activate {% endif %}">{{pg}}</a>
{% endfor %}
</div>
</div>
These are the codes that I entered after the ending of for loop.
What should I do to fix this error?
What’s available to the Template.render()
[django-docs] method is Context({'pr': products})
[django-docs] as seen here:
render(request, 'index.html', {'ct': cat, 'pr': products})
Therefore the Context
variable referring to the Paginator products
is pr
. This variable is what you should use to iterate over the Paginator products
in your template.
<div class="text-center">
{% for pg in pr.paginator.page_range %}
<a href="?page={{pg}}" class="btn btn-light btn-sm {% if pr.number == pg %} activate {% endif %}">{{pg}}</a >
{% endfor %}
</div>
Aside: the nomenclature of your variables is very weird. You need to improve on that so that your code will be readable.