<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Prajakta Mane on Medium]]></title>
        <description><![CDATA[Stories by Prajakta Mane on Medium]]></description>
        <link>https://medium.com/@maneprajakta?source=rss-aaece2ca997b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*zPylJqMX2vudliPtcDbBpQ.jpeg</url>
            <title>Stories by Prajakta Mane on Medium</title>
            <link>https://medium.com/@maneprajakta?source=rss-aaece2ca997b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 15:53:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maneprajakta/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Searching is Done in Django?]]></title>
            <link>https://medium.com/dsc-dypcoe/searching-in-django-8384c1c65b41?source=rss-aaece2ca997b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8384c1c65b41</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Prajakta Mane]]></dc:creator>
            <pubDate>Tue, 05 May 2020 10:29:09 GMT</pubDate>
            <atom:updated>2020-05-05T13:40:36.075Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*muDAVXRX2uyJUfTJ" /><figcaption>Photo by <a href="https://unsplash.com/@hishahadat?utm_source=medium&amp;utm_medium=referral">Shahadat Rahman</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h4><em>In this article, I’ll explain how to make a search bar in Django which performs a search on the Django database and provides the result.</em></h4><p>In order to perform search functionality in Django, we need to perform a query on the model database which is similar to any SQL query we perform.</p><p>To perform a query on database Django provides Q objects which can be imported from <em>django.db.models.</em></p><p>In short Q object encapsulates a SQL expression in a Python object that can be used in database-related operations. Using Q objects we can make complex queries with less and simple code <a href="https://micropyramid.com/blog/querying-with-django-q-objects/">for more on Q objects</a>.</p><h4><strong>Let’s get started :</strong></h4><p>Here is what <strong>models.py</strong> look like :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e55763527ae0764624d1c93609ca153b/href">https://medium.com/media/e55763527ae0764624d1c93609ca153b/href</a></iframe><p>We have a model blog with attributes blog_author ,blog_topic and pages the thing to remember is that each attribute of a model represents a database field. So, we have a database-table blog with the column as an attribute of the model.</p><p><strong>Problem</strong>: We want to build a simple search feature which will search for a blog entry in the blog model database based on the author of blog or topic of blog or pages in the blog.</p><p><strong>Solution:</strong></p><ol><li>First, we will look at our template file <strong>index.html.</strong> How it will send a request to <strong>blogSearch</strong> function in <strong>views.py </strong>to look for whether the searched field is in our database if yes than render it and if not display a not found message.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a06e472438e9668ea8b48a60903d8350/href">https://medium.com/media/a06e472438e9668ea8b48a60903d8350/href</a></iframe><ul><li>To reference data when a form is submitted name field of input tag is used. So, here name= <em>“srch”</em> will store the data for which search is made.</li><li>After submitting the form as the method is POST and action /blogSearch/ it will refer to blogSearch in views.py which perform the functionality of searching.</li></ul><p>2. Second our <strong>views.py </strong>it contains blogSearch .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b2d8026536fb12fc116c17248baef71/href">https://medium.com/media/3b2d8026536fb12fc116c17248baef71/href</a></iframe><p>How blogSearch works?:</p><ol><li>It checks for request first if it is ‘<strong>POST</strong>’:</li></ol><ul><li>Then it stores data referenced by name field of input tag to search variable.</li><li>If it is not null it will perform query and store result in a match .</li></ul><pre>match=Blog.objects.filter(Q(blog_author__icontains=search)|Q(blog_topic__icontains=search)|Q(pages__icontains=search))</pre><pre>equivalent SQL:<br><strong>SELECT</strong> * <strong>from</strong> blogs <strong>where</strong> <br>    blog_author <strong>LIKE</strong> &#39;%search%&#39; <strong>OR</strong> blog_topic <strong>LIKE</strong> &#39;%search%&#39; <strong>OR</strong> <br>    pages <strong>LIKE</strong> &#39;%search%&#39;;</pre><p>__icontaing is not in new Django documentation <a href="https://docs.djangoproject.com/en/2.1/ref/models/querysets/#icontains">refer this for more</a></p><ul><li>If query or search is successful than objects which match are rendered to index.html</li><li>else error message is returned to index.html</li></ul><p>2. For any other failure, we will be returned back to the same page.</p><p>How the final page will look:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*opXh0Gdsci8ze8K7jWuIWw.png" /><figcaption>index.html home page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rZ6UhNuGddzPVwzkJPox7Q.png" /><figcaption>Result of search - c++</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vw7BLtB20bZwinfhOJsO0w.png" /><figcaption>No result found when entered the wrong field</figcaption></figure><p>Yeah! It’s working as expected 😍 Thus we’ve successfully implemented searching in Django! 🚀</p><p><strong><em>You can find source code below:</em></strong></p><p><a href="https://drive.google.com/drive/folders/1jsP5rRAGaZNcNrhM6ednVPb9LcTT0Q7n">blog - Google Drive</a></p><blockquote>If you found this article helpful then please share it with everyone. Maybe it’ll be helpful for someone needy!</blockquote><blockquote>Sharing is Caring!</blockquote><p>Thank you! 😃</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8384c1c65b41" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dsc-dypcoe/searching-in-django-8384c1c65b41">How Searching is Done in Django🔍?</a> was originally published in <a href="https://medium.com/dsc-dypcoe">GDSC DYPCOE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>