I want to apply pagination on my data I tried to watch lots of videos and read lots of articles but still can't solve my problem. This is my Views.
def car(request):
all_products = None
all_category = category.get_all_category()
categoryid = request.GET.get('category')
if categoryid:
all_products = Product.get_all_products_by_id(categoryid)
else:
all_products = Product.get_all_products()
data = {}
data['products'] = all_products # all products
data['category'] = all_category # all category
all_products = Product.get_all_products()
data['product'] = all_products
]
return render(request, 'car.html', data)
as you can see I made some changes in above code but its make no diffrence
def car(request):
all_products = None
all_category = category.get_all_category()
categoryid = request.GET.get('category')
if categoryid:
all_products = Product.get_all_products_by_id(categoryid)
else:
all_products = Product.get_all_products()
#pagination
paginator = Paginator(all_products,2) **Changes**
page_number=request.GET.get('page') **Changes**
finaldata=paginator.get_page(page_number) **Changes**
data = {'all_products':finaldata,} **Changes**
data['products'] = all_products #all products
data['category'] = all_category #all category
all_products = Product.get_all_products()
data['product'] = all_products
return render(request, 'car.html', data)
I want to display 4 products per page I tried to apply data limit query that work but that not a genuine approach to display data. I read many articles and watch YouTube video. but can't find any solution. which videos and articles I watched there pagination method is totally different they use pagination with objects.all method to get all data and I used .get method to get data I think that is my problem. and second thing is that they just working with simple data to paginate but in my case it's so complicated. I tried alot please guide. I got stuck in solving a problem for 5 days now. I am convinced that I'm not a good programmer. I tried a lot but I can't succussed.
CodePudding user response:
My problem is almost solve there is one issue. I can't get the next, previous and last option in pagination but soon I'll figure it out. this is my views.py file coding
def car(request):
all_products = None
all_category = category.get_all_category()
categoryid = request.GET.get('category')
if categoryid:
all_products = Product.get_all_products_by_id(categoryid)
else:
all_products = Product.get_all_products()
#pagination
paginator = Paginator(all_products,3)
page_number=request.GET.get('page')
finaldata=paginator.get_page(page_number)
totalpage=finaldata.paginator.num_pages
data = {'all_products':finaldata,
'lastpage':totalpage,
'totalPageList':[n 1 for n in range(totalpage)]}
data['products'] = finaldata #all products
data['category'] = all_category #all category
all_products = Product.get_all_products()
data['product'] = all_products
return render(request, 'car.html', data)
This is my html coding my html file screenshot I'm sharing html screenshot due to this error Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL K keyboard shortcut. For more editing help, click the [?] toolbar icon. It looks like your post is mostly code; please add some more details.
I'm not familiar with stack overflow so I don't know what to do so I shared screenshot
when I find any solution for first previous and last option then soon I'll share that here
CodePudding user response:
Based on pagination documentation, it would be something similar to this: (Always explore the framework documentation, it is the best source to learn)
views.py
from django.core.paginator import Paginator
from django.shortcuts import render
def car(request, category_id=None):
products = Products.objects.all()
if category_id:
products = Product.objects.filter(category__id=category_id)
paginator = Paginator(products, 3)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'page_obj': page_obj
}
return render(request, 'car.html', context)
tempate.html (where you access previous and next pages, and total number of pages)
{% for car in page_obj %}
{{ car.category }}<br>
...
{% endfor %}
<div >
<span >
{% if page_obj.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ page_obj.previous_page_number }}">previous</a>
{% endif %}
<span >
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}.
</span>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}">next</a>
<a href="?page={{ page_obj.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
urls.py (you want to pass category_id, if you don't it will be none)
path('cars/category/<int:category_id>/', views.car, name='car'),