class PersonList(ListView):
model = Person
template_name = 'your-template-name.html'
context_object_name='persons'
def get_queryset(self):
search_string = self.request.GET.get('search',None)
if search_string is None:
persons = Person.objects.filter()
else:
persons = Person.objects.filter(name__icontains=search_string)enter code here
return persons
В ваш шаблон необходимо добавить это.
<form class="form-inline my-2 my-lg-0" action="{% url 'artdb:search' %}" method="get">{% csrf_token %}
<input class="form-control mr-sm-2" type="text" placeholder="Search" name="search" aria-label="Search" value="{{search}}">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
В вашем urls.py
из URL импорта django.conf.urls
urlpatterns = [
url (r '^ search-result $', views.PersonList.as_view (), name = 'search'),
]