How to Perform Multisearch in Django Application

Posted on


This is an article for describing about how to do or to perform multisearch in Django application. The term multisearch in this context has a specific context. It means every keyword search is up for the searching process in multi field of column. In this case, a keyword search can be available in one or more columns which in another word it can be available in multi column. So, it looks like a multi search in more than one column using just that keyword. How does it look like to do it in Django application. The following is the actual preparation in order to do that :

  1. First of all, there is a Django-application exist.

  2. Moreover, there is a page definition to accommodate the search function. That page consists of several parts which are important in order for the search function to run properly.

  3. The first one is the URL address for accessing the page containing the search function. It exist in the ‘’ file inside the root folder of the application. The following is the content for that URL address definition :

    path("search/", views.multi_search, name="search"),
  4. The second one is to check the function with the name of ‘multi_search’ as it is defined in the ‘’ file as the execution target. It exist in the ‘’ file where it is available at the root of the application’s folder as follows :

    from django.db.models.fields import NullBooleanField
    from django.shortcuts import render
    from org.models import Employee, Unit
    from django.db.models import Q
    def multi_search(request):
        if request.method == 'GET':
            keyword = request.GET.get('keyword')
            submitbutton= request.GET.get('submit')
            if keyword is not None:
                lookups= Q(id_number__icontains=keyword) | Q(name__icontains=keyword)
                results= Employee.objects.filter(lookups).distinct()
                context={'results': results,
                         'submitbutton': submitbutton}
                return render(request, 'org/search.html', context)
                return render(request, 'org/search.html')
            return render(request, 'org/search.html')

    The function with the name ‘multi_search’ above will check for any GET request method. If it is exist, it will try to get the string of characters from the text component with the name of ‘keyword’. That text component will pass the value into a variable with the name of ‘keyword’. Furthermore, if there is a value stored to that variable which is not None, just process it. The process is using the ‘Q’. Atually, Q object is encapsulating an SQL expression in a Python object so that it can be used in a database-related query operations.

  5. Next, carry on to another important part which is the HTML file. In order to display the search form and also the result of the search process, it need an HTML file to place the multi_search function. It is an HTML file with the name of ‘search.html’. The following is the content of the  HTML file  :

    {% extends "layouts/base.html" %} {% block title %} {{ title }} {% endblock %}
    {% block content %}
    <!-- Specific Page CSS goes HERE  -->
    {% block stylesheets %}{% endblock stylesheets %}
    <div class="content">
      <div class="card">
        <div class="card-header">Search Page</div>
        <div class="card-body">
          <h5 class="card-title">Search Form</h5>
          <p class="card-text">
            Search help for employee based on Name and Employee ID.
            action="{% url 'search' %}"
            <input type="submit" name="submit" value="Search" />
          {% if submitbutton == 'Search' and request.GET.keyword != '' %}
          <h5>Search Result for : <b>{{ request.GET.keyword }}</b></h5>
          <br /><br />
            <table class="table">
                <th scope="col">#</th>
                <th scope="col">ID Employee</th>
                <th scope="col">Name</th>
            {% if results %}
              {% for result in results %}
                <th scope="row">#</th>
            {% endfor %}{% else %}
                <th colspan=3>Result Not Found</th>
            {% endif %}
          {% endif %}
        {% endblock %}

    In summary, the HTML file will display one text field component with the name of ‘keyword’. If anyone type a string of characters in that text field component, it will trigger the GET request method to receive that string of characters for further processing. For an example, it will try to search in just two columns or two fields of search criteria. That two columns is the ‘id_employee’ and ‘name’. If in those columns contain those string of characters, it will immediately display the search result in the form of tables. If there are no match at all, it will display the information that the Result Not Found.

One thought on “How to Perform Multisearch in Django Application

Leave a Reply