Im using seperate py models for ecommerce website. But i cant use search and pagination functions

`search.py

from django.shortcuts import render , redirect , HttpResponseRedirect
from store.models.product import Product
from django.views import View
from django.core.paginator import Paginator,PageNotAnInteger, EmptyPage
from django.db.models import Q

class Search(View):
def src(request):
search_post = request.GET.get(‘search’)
if search_post:
posts = Product.objects.filter(Q(MALINCINSI__icontains=search_post) & Q(MALINCINSI__icontains=search_post))

else:
# If not searched, return default posts
posts = Product.objects.all()
posts = Product.objects.all()

def pgn(request):  
  page = request.GET.get('page', 1)

  paginator = Paginator(posts,per_page=300)
  page_range = paginator.get_elided_page_range(number=page)
  
  try:
     posts = paginator.page(page)
     
  except PageNotAnInteger:
     posts = paginator.page(1)
  except EmptyPage:
     posts = paginator.page(paginator.num_pages)
  return render(request, 'base.html', {'posts': posts})

Try to use them in one function

def search(request):
    qs = YOUR_QUERY 
    ## next lines for pagination
    paginator = Paginator(qs, 5)
    page = request.GET.get("page")

    try:
        main_page = paginator.page(page)
    except PageNotAnInteger:
        ## If page is not an integer deliver the first page
        main_page = paginator.page(1)
    except EmptyPage:
        ## If page is out of range deliver last page of results
        main_page = paginator.page(paginator.num_pages)

    context = {
        "qs": qs,
        "page": page,
        "main_page": main_page,
    }

I think you may forget to pass “page” into context dictionary with “posts”
hope it helps

1 Like

thanks for help. i will try it and share outcomes

Well not working. can you check it out if i share with project with you? Cause im not making any progress almost a month

Pagination has a special template, Do you have the pagination template or not?
Also you can share your code with us,
If I can help, I will not be hesitated

1 Like

Thanks for help. I created seperate views and models. So pagination has own view file. Also created html file for it but not working. Now trying js function for search and pagination in same html file. If not work, i will share whole project

Okay I wish you succeed.
I’ll recommend this js table like( data table )

  • you can begin from here
  • Data tables (forums and all about it)
    I know that data table is sometimes difficult but it is helpful give it a try and you will thank me
    Finally if you need the pagination template I can share it with you
    Good luck
1 Like

You can use Django-tables2 which is solid and has been around for many years and still going strong.

Or if you feel more adventurous use open source Tabulator JavaScript library it has a Python package to make integrating it in your Django template a bit easier.

Tabulator has out of the box filter and sorting build in. Have been experimenting with it recently and it works very well. On the homepage you find many code examples.

2 Likes

thanks a lot man. I will try and share results

Thanks man. First time hearing tabulator. I will work on it and let you know

Thanks @dennisvd
I have worked with Django-Tables2 really it’s amazing package but our business needs lead me to js data table (because it makes auto search for all query fields)
this helps me a lot and saves many times.
Tabulator it’s new package I haven’t worked before but I will give it a try
Thanks again dennisvd