I want to create pagination in Django?
This is my view.py coding. it was working but when I add pagination then some error occurred
def car(request):
all_products = None
all_category = category.get_all_category()
categoryid = request.GET.get('category')
paginator=Paginator(categoryid,4)
page=request.GET.get('page')
try:
all_category.paginator.page(page)
except PageNotAnInteger:
all_category=paginator.page(1)
except EmptyPage:
all_category=paginator.page(paginator.num_pages)
else:
all_category = Product.get_all_products()
if categoryid:
all_products = categoryid.get_all_products_by_id(categoryid)
data = {}
data['products'] = all_products
data['category'] = all_category
all_location = location.get_all_location()
all_products = Product.get_all_products()
all_team = teams.get_all_team_members()
data['product'] = all_products
data['location'] = all_location
data['teams'] = all_team
return render(request, 'car.html', data, {'all_category':all_category,
'categoryid':categoryid,
'category':category(),
'page':page})
**This is my pagination.html file **
<div class="">
<span class="">
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}">Previous</a>
{% endif %}
<span class="">
Page {{ page.number }} of {{ page.paginator.num_pages }}.
</span>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}">Next</a>
{% endif %}
</span>
</div>
This is my car.html file.
<div class="container-fluid py-1 justify-content-between p-2 m-2">
<div class="container">
<h6 class="display-4 text-uppercase text-center mb-5" style="font-size: 40px">Filters</h6>
{% for category in category %}
<a href="{% url 'car' %}?category={{ category.id }}">
<button type="button" class="btn btn-outline-secondary rounded">{{ category.name }}</button>
</a>
{% endfor %}<hr>
<h1>{% include 'pagination.html' with page=all_category %}</h1>
</div>
</div>
Actually I want to display all cars data by pagination same all cars categories by patination.
I am following this tutorial text
My question is that where I've make mistake.
This is an error
^^^^^^^^^^^^^^^^^^^^^
File "C:\Python311\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "E:\kashi\CarRental\CarRental\views.py", line 48, in car all_category.paginator.page(page) ^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'QuerySet' object has no attribute 'paginator' [27/Nov/2022 21:50:28] "GET /car/ HTTP/1.1" 500 64248 enter image description here