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 Elasticsearch, 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.

Python Code below follows the video to help :

Transcript/Cheat-sheet :
 
Templates.py:
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 :



<div>
<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>
</form></div>

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)
print(search)
print(uid)
timestamps = database.child(users).child(uid).child(reports).shallow().get().val()
work_id=[]
for i in timestamps:
wor = database.child(users).child(uid).child(reports).child(i).child(work).get().val()
wor = str(wor)+$+str(i)
work_id.append(wor)
matching = [str(string) for string in work_id if search in string.lower()]
s_work=[]
s_id=[]
for i in matching:
work,ids=i.split($)
s_work.append(work)
s_id.append(ids)
print(s_work)
print(s_id)
date = []
import datetime
for i in s_id:
i = float(i)
dat = datetime.datetime.fromtimestamp(i).strftime(%H:%M %d-%m-%Y)
date.append(dat)
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 .

Urls.py

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 = [
url(r^check/,views.check,name=check),
]
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.