When your Django site grows, adding search becomes essential to improve navigation and enhance Django auto admin registration for better content management.
In this article, I’ll walk you through how to build a basic search feature in Django using nothing but Python, templates, and Django’s ORM.
Prerequisite: A Working Django Project
If you haven’t already set up a Django project and app, start here:
How to Build Your First Django Site (Step-by-Step)
Make sure you’ve got:
- A Django project set up
- A model (e.g., Post)
- Some sample data
- A view displaying those model entries
We’ll be adding a search box to filter these posts by title or content.
Add a Search Form to Your Template
Let’s start with the front-end. Open your template (index.html
or wherever you list content) and add a basic HTML form.
<form method="get" action="">
<input type="text" name="q" placeholder="Search posts...">
<button type="submit">Search</button>
</form>
- method="get" lets the search query appear in the URL.
- name="q" is the name of the search term we’ll capture in our view.
Place it above your content list.
Capture the Search Term in the View
Now update your Django view to filter results.
from django.shortcuts import render
from .models import Post
from django.db.models import Q
def home(request):
query = request.GET.get('q')
if query:
posts = Post.objects.filter(
Q(title__icontains=query) | Q(body__icontains=query)
)
else:
posts = Post.objects.all()
return render(request, 'blog/index.html', {'posts': posts, 'query': query})
- request.GET.get('q') gets the search term from the URL.
- Q() lets us filter multiple fields with OR logic.
- icontains = case-insensitive match.
Show the Search Term in the Template (Optional)
To show what’s being searched:
{% if query %}
<p>Showing results for "<strong>{{ query }}</strong>"</p>
{% endif %}
Test It Out
Run the server:
python manage.py runserver
Try words that appear in your titles or bodies. If there’s a match — you’ll see them filtered. If not — nothing appears. You can always add a fallback:
{% if posts %}
{% for post in posts %}
<!-- your post display here -->
{% endfor %}
{% else %}
<p>No posts found.</p>
{% endif %}
Bonus: Highlight Matches (Optional UX Touch)
For an extra UX boost, you can highlight the search term inside the results. Here’s a very basic example using Django’s built-in safe
filter:
# In your view:
import re
def highlight(text, word):
pattern = re.compile(re.escape(word), re.IGNORECASE)
return pattern.sub(f'<mark>{word}</mark>', text)
# Apply in your view loop (optional, for power users only)
What’s Next?
This search feature is just the beginning. You can now:
- Add Pagination so users can browse search results easily
- Filter Search by Author or Tag (coming soon)
- Build a Search API with Django Rest Framework
- Combine it with User Login for personalized results
And if you want to turn your app into a real project with a navbar, routes, and more, don’t miss: How to Build a Django Site from Scratch
If you’re planning to scale, here are some future options:
- Use PostgreSQL full-text search
- Add autocomplete with JavaScript
- Integrate external tools like Elasticsearch or Meilisearch
But for 90% of small to mid-size projects, what you’ve just built is more than enough.
Final Thoughts
Adding search to Django doesn’t need a plugin or frontend framework. Just a form, some filtering logic, and the ability to deliver results clearly.
If this helped you, check out:
- Django Pagination Made Simple
- How I Used Django Admin to Manage Blog Posts
- User Authentication Without the Mess