Skip to content Skip to sidebar Skip to footer

Django Paginate For Django 2

I need to use pagination to a Django list but I couldn't find any help online,, only old docs from Django version 1.3 here are my files : views.py def home(request): all_dres

Solution 1:

Found the full doc and clear way to do it on this link https://docs.djangoproject.com/en/2.0/topics/pagination/

Solution 2:

import paginator in views.py

from django.core.paginatorimportEmptyPage, PageNotAnInteger, Paginator

function:

defhome(request):
all_dress = Item.objects.all().filter(dress_active=True)
all_good_dress = Item.objects.all().filter(dress_special=True)
current_user = request.user
paginator  = Paginator(all_dress, 10)
page = request.GET.get('page')

try: 
    all_dress = paginator.page(page)
except PageNotAnInteger:
    all_dress = paginator.page(1)
except EmptyPage:
    all_dress = paginator.page(Paginator.num_pages)
context = {
    'all_dress': all_dress,
    'current_user': current_user,
    'all_good_dress':all_good_dress,
}
return render(request, 'fostania/home.html', context)

Template: enter image description here

and one thing if user login you can direct acces his information in template like {{request.user.email}}

Post a Comment for "Django Paginate For Django 2"