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 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
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
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
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
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.
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