Header Ads

Python django with Firebase Tutorial : Create Search bar for Firebase database #Part10

Here i continue teaching  Python Django with Google Firebase series, In this post i will show you how to integrate Search bar in Python Django for searching in firebase database. For implementing this we are having different ways such as Using Nested Loops , Using Elastic Search, Using Queries. One of the drawback of firebase database is we can't use any Queries like we use in SQL. In this tutorial we will use concept of nested looping using Pyrebase library for creating a search bar.

Code below follows the video to help :

Transcript/Cheat-sheet :


So, first let's start with Front-end part, for a basic implementation we need  a search box and a button to trigger the search function defined our views.py for searching the database and retrieve the results to Django template. We can also implement this search asynchronously by using jquery then we can get suggestions and there will be no need for button a search button. If you want let me know in comments i will create a detailed tutorial on it. 

Code :

<form method="get" action="/check" id="s">
{% csrf_token %}
<input type="text" placeholder="search..." name="search">
<input type="hidden" value="{{uid}}" >
<button type="submit" onclick="location.href='/check/?uid={{uid}}'" form="s">Go</button>
<button type="button" onclick="location.href='{% url 'log' %}'">Logout</button>

Views.py :

Now, here comes the main part we need to get the search string entered in search bar and search it out in firebase database using nested loops and pyrebase library shallow function.

Steps :

  1. Using Get operation extract the search keyword.
  2. Create a search function : here we are going to use our existing function created previously for retrieving the data from firebase database
  3. Use Pyrebase library shallow function for just extracting the keys from database i.e. timestamps. [shallow.get().val()]
  4. In every timestamp getting value of work key as we need to search within work according to our use-case. [get().val()]
  5. Matching the values using if statement, if matches append to a list.
  6. Mapping the list data to Django template.
Code : 

def check(request):
if request.method == 'GET' and 'csrfmiddlewaretoken' in request.GET:
search = request.GET.get('search')
search = search.lower()
uid = request.GET.get('uid')
timestamps = database.child('users').child(uid).child('reports').shallow().get().val()
for i in timestamps:
wor = database.child('users').child(uid).child('reports').child(i).child('work').get().val()
wor = str(wor)+"$"+str(i)
matching = [str(string) for string in work_id if search in string.lower()]
for i in matching:
date = []
import datetime
for i in s_id:
i = float(i)
dat = datetime.datetime.fromtimestamp(i).strftime('%H:%M %d-%m-%Y')
comb_lis = zip(s_id, date, s_work)
name = database.child('users').child(uid).child('details').child('name').get().val()
return render(request, 'check.html', {'comb_lis': comb_lis, 'e': name, 'uid': uid})

Above code includes only if statement, for else statement code you can have a look on our previous post .


In this file we need to add url for accessing this search functionality function.

from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
So, wrapping up at last we have successfully implemented search bar in python django for searching values in firebase database and map the result to django template. 


  1. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!
    Web Design Training

  2. Really nice experience you have. Thank you for sharing. It will surely be an experience to someone.
    Python Online training
    Python Course institute in Bangalore

  3. This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
    python Training institute in Chennai
    python Training institute in Bangalore


Powered by Blogger.