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

  4. Thank you so much for helping in coding,
    i also apply some codes here visit Fishing Life MOD Apk 2019

  5. Hiring a SEO agency that has been in the game for a while will also provide you with the added advantage of not having to worry about them using any SEO techniques while working on your business website.SEO companies in Delhi NCR

  6. SEO PowerSuite is a package which can help you with each factor of SEO to enable you to improve the search results overall performance of one's web site. This particular SEO PowerSuite review provides you with an in depth information with the benefits and features provided by SEO PowerSuite.Webdesign

  7. I have gain a lot of information's from your Articles and Looking For more Posts...Thanks for this Information's about Python
    python training in chennai | python training in annanagar | python training in omr | python training in porur | python training in tambaram | python training in velachery

  8. The skills in SEO also keep changing. If you decide to do the SEO of your site In-House, then you need to spend time and sometimes money to learn the new aspects of SEO and keep yourself updated with the changes. the best affiliate programs for beginners


Powered by Blogger.