I am following a udemy course where I am making a real estate website using Django. I have come so far and have been fighting with the errors but I am stuck with the pagination error that I am facing right now.
I see everything is working fine in the video but when I follow it exactly it does not work. The issue is that when I set a limit on items per page, it does not seem to be working as I see all the results on the same page.
Here is my views.py
from django.shortcuts import render
from django.core.paginator import Paginator
from .models import Listing
def index(request):
listings = Listing.objects.all().order_by('-id')
paginator = Paginator(listings, 3)
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'listings': listings
}
return render(request, 'listings/listings.html', context)
It's my Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='listings'),
path('<int:listings_id>', views.listing, name='listing'),
path('search', views.search, name='search')
As you can see, I have set maximum 3 listings per page but I am getting all of them on a single page which is not what I want. It would be great if anyone could help. Thanks for your time and efforts.
You are returning whole listings instead of Paginator page inside of context
context = {
'listings': page_obj
}
More regarding pagination you can check also in documentation
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.