Handle a search query in Django

In our views.py we define the following:

from myapp.models import MyModel

def my_view(request):
  instances = MyModel.objects.all() # This does not have to be like that, another kind of query can be applied
  query = request.GET.get('q') # 'q' -> name in search input (see html form)
  if query:
    instances = instances.filter(Q(title__icontains=query) | Q(description__icontains=query))
  ## continue with other stuff, creating the context, rendering templates, so on.

In our HTML, we need a form with a GET method:

<form method="get">
  <input type="search" name="q" />
  <button type="submit">Search</button>

Keep the query in your pagination:

<a>href="?page=X{%if request.GET.q%}&q={{ request.GET.q }}{%endif%}">Page X</a>

πŸ“Š Create a modern CV
Create a modern CV

Create a modern CV to impress recruiters