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 :
|
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.
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 :
- Using Get operation extract the search keyword.
- Create a search function : here we are going to use our existing function created previously for retrieving the data from firebase database.
- Use Pyrebase library shallow function for just extracting the keys from database i.e. timestamps. [shallow.get().val()]
- In every timestamp getting value of work key as we need to search within work according to our use-case. [get().val()]
- Matching the values using if statement, if matches append to a list.
- Mapping the list data to Django template.
Code :
|
||
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.
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
Thank you so much for helping in coding,
i also apply some codes here visit Fishing Life MOD Apk 2019
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
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