We'll create a fast and simple database search using Django and HTMX. It's easy and fast to do with HTMX.
There'll be 6 steps, plus a bonus at the end.
Here's a optional video tutorial (with me 👋) that follows the written guide below.
If you include your bonus, your final product will look like below. Let's go 🚀
First, create a new Django project and a new app. I'm calling my app 'sim'.
In settings.py, add the new app:
Run the migrations:
Create the view for the search functionality.
- Create a new folder called `templates` inside sim
- Add a file search.html inside templates:
Add a file search_results.html inside templates:
Include this in the project's main urls.py:
Open your django shell from the terminal with:
Then in the shell:
Run the server.
Visit http://127.0.0.1:8000/search/ to see your instant search in action.
All done 🎉 You now know how to add instant search to Django 🐎 This is a simple type of search that's entirely unoptimised, but is great for starting.
My advice would be to avoid optimising your search completely until it starts to slow. I'd think about optimising only then to avoid premature optimisation (my wolf's bane).