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 :

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. 






9 comments:

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

Powered by Blogger.