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 search(request):
search_post = request.GET.get('search')
if search_post:
posts = Product.objects.filter(Q(name__icontains=search_post) & Q(name__icontains=search_post))
else:
# If not searched, return default posts
products = Product.objects.all()
page = request.GET.get('page', 1)
paginator = Paginator(products,per_page=300)
page_range = paginator.get_elided_page_range(number=page)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
return render(request, 'base.html', {'posts': posts})
HTML file like this:
<form class="form-inline my-2 my-lg-0" action="">
<input class="form-control mr-sm-2" type="search" [A-Za-z0-9 ]
onkeyup="this.value = this.value.toUpperCase();"
onkeydown="return /[a-z]/i.test(event.key)"
placeholder="(ÖÇİŞĞÜ)kullanmayın!!" aria-label="Search" name="search">
<button class="btn btn-outline-success my-2 my-sm-0"
type="submit">ARA</button>
</form>
</div>
{% if posts.has_other_pages %}
<ul class="pagination">
{% if posts.has_previous %}
<li><a href="?page={{ posts.previous_page_number |add:'-2' }}">«</a></li>
{% else %}
<li class="disabled"><span>«</span></li>
{% endif %}
{% for i in posts.paginator.page_range %}
{% if posts.number == i %}
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if posts.has_next %}
<li><a href="?page={{ posts.next_page_number |add:'-2' }}">»</a></li>
{% else %}
<li class="disabled"><span>»</span></li>
{% endif %}
</ul>
{% endif %}
Side note: When posting code or templates here, enclose the code (or template) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing your post to add these. Please remember to do this in the future.)
Please provide more details about precisely what you mean by “not working”. What is happening that you don’t expect to have happen? (Or, what is not happening that you are expecting to see?)
Note: You have
However, you shouldn’t have a space between the variable name and the '|'
in the template.