Implementing Search functionality in your Django website

Implementing Search functionality in your Django website

Most of the time, the reason for me to visit a particular website is to look up something and without a search option on the website, it will be a whole mess for me. I know you will say the same too. So to make our website more appealing and easy to use for the user, search functionality is a must. In this post, I will explain how you can add the search functionality in an easy way.

For this post, I will be taking my blog project as an example and will try to add search functionality so that a user can easily find a certain blog they are looking for. Let's get started!

Firstly let's configure the search bar in the frontend :

Here's what we are doing -> We set the method to POST, set the action to redirect the user to the URL file that we will be creating later, added csrf_token for security purposes, and lastly gave a name to the search bar for future references.

<!-- templates/_home.html -->
<!--Set the action to redirect to search and gave a name to the search input for future use. --> 
<form class="d-flex" method="POST" action="{% url 'search' %}">
    {% csrf_token %}
    <input class="form-control pull-right" type="text" placeholder="Search" aria- 
        label="Search" name="search_articles">
</form>

Now that we are all set with the search bar in the frontend, let's hop on the views file and make a view for our search functionality.

# blog/views.py
# Search is a function based view that shows the filtered articles whenever the search form is submitted.
def search(request):
    search_articles=request.POST['search_articles']
    articles_found=ArticleModel.objects.filter(title__icontains=search_articles)
    return render(request,'_search.html',{'search_articles':search_articles,'articles_found':articles_found})

Here's what happens in the code -> We first receive whatever the user wrote in the search bar using request.POST, and then we stored it in a variable named search_articles, after that we use __icontains in the Django filter to filter out all the objects from ArticleModel(i.e articles) which contain the searched query. And after getting all the articles containing the given query we render the search page giving searched query and the set of articles we filtered as context.

Now that we are done with defining the views for search, we need to provide an URL where it will be displayed. Let's head towards the urls.py file :

#blog/urls.py
path('search/',views.search,
         name='search'),

Now comes the final part, i.e the template for search. We already have the searched query and filtered articles from views (as context) , all we have to do is display them in the template.

<!--  templates/_search.html -->
<!-- Displaying the filtered articles we got from the views using context. -->

{% extends "posts/base.html" %}
{% block content %}
{% for post in articles_found%}


<div class="card article col-11 m-3" >
    <div class="row no-gutters">
      <div class="col-md-8">
        <div class="card-body text-left">
            <h5 class=" card-title">
              <strong> <a href="{% url 'post-detail' post.id %}" class="post_title"> {{post.title}} </a> </strong>
            </h5>
            <small class="text-muted"
              >Posted by <a href="{% url 'user-posts' post.author.username %}"> {{post.author}} </a> on {{post.post_date|date:"F d, Y"}}  </small
            >
          </div>

          <p class="card-text text-left ml-3">
            {{post.content|truncatechars:250}}
          </p>
          <p class="card-text">

          </p>
        </div>
      </div>
    </div>
{% endfor %}
{% endblock content %}

And there you have it. Working search functionality for your Django website. I hope this article helped you add that vital search feature to your Django website. Please comment with your valuable suggestions and feedback. In case you want to connect with me, follow the links below:

LinkedIn | GitHub | Twitter