CBV поиск в Django
Переписываю поиск в Джанго с FBV в CBV.
FBV Search:
def searchbar(request):
if request.method == "POST":
searched = request.POST["searched"]
students = Student.objects.filter(Q(first_name__contains=searched) | Q(last_name__contains=searched))
return render(request, template_name='tools/searchbar.html',
context={"searched": searched, "students": students})
CBV Search
class StudentSearchView(ListView):
model = Student
template_name = "tools/searchbar.html"
def get_queryset(self):
q = self.request.GET.get("q")
object_list = Student.objects.filter(Q(first_name__icontains=q) | Q(last_name__icontains=q))
return object_list
searchbar.html
{% extends "index.html" %}
{% block content %}
{% if q %}
<h1>Search Results</h1>
<br/>
{% for student in object_list %}
<li>
{{ student.first_name }} {{ student.last_name }}<br/>
</li>
{% endfor %}
{% else %}
<h1>Nothing to search</h1>
{% endif %}
{% endblock %}
index.html
<form class="form-inline" method="GET" action="{% url "searchbar" %}">
{% csrf_token %}
<div class="input-group mb-3">
<input class="form-control mr-sm-2" type="search" name="q"
placeholder="Search" aria-label="Search">
<button class="btn btn-primary" type="submit">Search</button>
</div>
</form>
Никакой ошибки не возвращает, при поиске перекидывается в блок else (searchbar.html ) и пишет что "Nothing to search". В чем может быть проблема? Заранее спасибо.