-1
Hello, I am trying to paginate the results of a search page on Django, but without success. A class based view is responsible:
class SearchResultsView(ListView):
model = User
paginate_by = 2
template_name = 'search.html'
def get_context_data(self, **kwargs):
query = self.request.GET.get('q')
context = super().get_context_data(**kwargs)
context['page_obj'] = User.objects.filter(name__icontains=query)
context['quant'] = User.objects.filter(name__icontains=query).count()
context['search'] = query
return context
Where 'q' is the user name the user searches for a search bar
{% extends 'base.html' %}
{% block content %}
<div class='col-md-6'>
<h2>Temos {{ quant }} usuários com o nome semelhante a "{{search}}"</h2>
<ul class="list-group">
{% for name in page_obj %}
<a href="{% url 'name' name.id%}" class="list-group-item list-group-item-action"> {{ name.name }}</a>
{% empty %}
<li><h2>Não achamos nada parecido, desculpe.</h2></li>
{% endfor %}
</ul>
{% if is_paginated %}
<ul class="pagination justify-content-center" style="margin-top: 10px;">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" tabindex="-1">Previous</a>
</li>
{% endif %}
{% for num in paginator.page_range %}
{% if page_obj.number == num %}
<li class="page-item active"><a class="page-link" href="#">{{num}}</a></li>
{% else %}
<li class="page-item"><a class="page-link" href="?page={{num}}">{{num}}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}">Next</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endblock %}
The first page loads normally, the second page gives the following error:
Valueerror at /search/ Cannot use None as a query value