Introduction
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 :
-
First of all, there is a Django-application exist.
-
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.
-
The first one is the URL address for accessing the page containing the search function. It exist in the ‘urls.py’ 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"),
-
The second one is to check the function with the name of ‘multi_search’ as it is defined in the ‘urls.py’ file as the execution target. It exist in the ‘views.py’ 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) else: return render(request, 'org/search.html') else: 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.
-
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. </p> <form action="{% url 'search' %}" method="GET" value="{{request.GET.q}}" > Search <input type="text" name="keyword" value="{{request.GET.q}}" placeholder="Search" /> <input type="submit" name="submit" value="Search" /> </form> {% if submitbutton == 'Search' and request.GET.keyword != '' %} <h5>Search Result for : <b>{{ request.GET.keyword }}</b></h5> <br /><br /> <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">ID Employee</th> <th scope="col">Name</th> </tr> </thead> <tbody> {% if results %} {% for result in results %} <tr> <th scope="row">#</th> <td>{{result.id_employee}}</td> <td>{{result.name}}</td> </tr> {% endfor %}{% else %} <tr> <th colspan=3>Result Not Found</th> </tr> {% endif %} </tbody> </table> {% endif %} </div> {% endblock %} </div> </div>
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”