📜  从 Firebase 在 Django 中搜索数据

📅  最后修改于: 2022-05-13 01:55:45.467000             🧑  作者: Mango

从 Firebase 在 Django 中搜索数据

Firebase是 Google 的产品,可帮助开发人员轻松构建、管理和发展他们的应用程序。它可以帮助开发人员以更安全的方式更快地构建他们的应用程序。 firebase 方面不需要编程,这使得更有效地使用其功能变得容易。它提供云存储。它使用 NoSQL 来存储数据。

在这里,我们将学习如何在 Firebase 中搜索数据。为此,请执行以下步骤:

第 1 步:如果您是 firebase 新手,请参阅此内容。

步骤 2:转到urls.py文件并创建一个路径以移动到网页以搜索数据。

Python
from django.contrib import admin
from django.urls import path
from . import views
 
urlpatterns = [
   
    #when we are moving to search then move to this url
    path('search/', views.search),
   
    #showing search detail on this url
    path('searchusers/', views.searchusers),
]


Python
from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
import pyrebase
 
config={
    
    "databaseURL": "*********************",
    "projectId": "*******************",
   
}
firebase=pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
 
# move to this search.html page to search for content
def search(request):
    return render(request, "search.html")
   
# after typing what to search this function will be called
def searchusers(request):
    value = request.POST.get('search')
     
    # if no value is given then render to search.h6tml
    if value =="":
        return render(request, "search.html")
    title = request.POST['category']
    if title =="":
        return render(request, "search.html")
    if value is None or title is None:
        print(value ,"Value",title)
        return render(request, "search.html")
    else:
        if title == "Users":
            data = database.child('users').shallow().get().val()
            uidlist = []
            requid = 'null'
             
            # append all the id in uidlist
            for i in data:
                uidlist.append(i)
                 
            # if we have find all the uid then
            # we will look for the one we need   
            for i in uidlist:
                val = database.child('users').child(i).child('name').get().val()
                val=val.lower()
                value=value.lower()
                print(val,value)
                 
                # if uid we want is value then
                # we will store that in requid
                if (val == value):
                    requid = i
            if requid=='null':
                return render(request, "search.html")
            print(requid)
             
            # then we will retrieve all the data related to that uid
            name = database.child('users').child(requid).child('name').get().val()
            course = database.child('users').child(requid).child('course').get().val()
            branch = database.child('users').child(requid).child('branch').get().val()
            img = database.child('users').child(requid).child('imgUrl').get().val()
            Name = []
            Name.append(name)
            Course = []
            Course.append(course)
            Branch = []
            Branch.append(branch)
            Image = []
            Image.append(img)
            comb_lis = zip(Name, Course, Branch, Image)
             
            # send all data in zip form to searchusers.html
            return render(request, "SearchUsers.html", {"comb_lis": comb_lis})


HTML
{% load static %}

   
      Search Page
      
      
      
      
      
      
      
   
   
      
         
            
               {% csrf_token %}                                                                             
         
      
   


HTML
{% load static %}

   
      User's List
      
      
      
      
      
      
   
   
      
         
            
                               {% for name,course,branch,image in comb_lis %}                

Here are the results:

               
                  Profile                   

Name: {{name}}

                  

Course: {{ course }},  {{ branch }} 

               
               {% endfor %}             
         
         
      
   


第 3 步:然后移动到views.py文件并编写以下函数以呈现到 html 页面。

Python

from django.shortcuts import render
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.decorators import login_required
import pyrebase
 
config={
    
    "databaseURL": "*********************",
    "projectId": "*******************",
   
}
firebase=pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
 
# move to this search.html page to search for content
def search(request):
    return render(request, "search.html")
   
# after typing what to search this function will be called
def searchusers(request):
    value = request.POST.get('search')
     
    # if no value is given then render to search.h6tml
    if value =="":
        return render(request, "search.html")
    title = request.POST['category']
    if title =="":
        return render(request, "search.html")
    if value is None or title is None:
        print(value ,"Value",title)
        return render(request, "search.html")
    else:
        if title == "Users":
            data = database.child('users').shallow().get().val()
            uidlist = []
            requid = 'null'
             
            # append all the id in uidlist
            for i in data:
                uidlist.append(i)
                 
            # if we have find all the uid then
            # we will look for the one we need   
            for i in uidlist:
                val = database.child('users').child(i).child('name').get().val()
                val=val.lower()
                value=value.lower()
                print(val,value)
                 
                # if uid we want is value then
                # we will store that in requid
                if (val == value):
                    requid = i
            if requid=='null':
                return render(request, "search.html")
            print(requid)
             
            # then we will retrieve all the data related to that uid
            name = database.child('users').child(requid).child('name').get().val()
            course = database.child('users').child(requid).child('course').get().val()
            branch = database.child('users').child(requid).child('branch').get().val()
            img = database.child('users').child(requid).child('imgUrl').get().val()
            Name = []
            Name.append(name)
            Course = []
            Course.append(course)
            Branch = []
            Branch.append(branch)
            Image = []
            Image.append(img)
            comb_lis = zip(Name, Course, Branch, Image)
             
            # send all data in zip form to searchusers.html
            return render(request, "SearchUsers.html", {"comb_lis": comb_lis})


第 4 步:然后我们将移动到search.html页面并编写以下代码来搜索 firebase 中的数据。注释写在里面,以便更好地理解它。

HTML

{% load static %}

   
      Search Page
      
      
      
      
      
      
      
   
   
      
         
            
               {% csrf_token %}                                                                             
         
      
   


第 5 步:然后我们将转到searchusers.html页面,它将在网页上显示检索到的数据,如输出所示。

HTML

{% load static %}

   
      User's List
      
      
      
      
      
      
   
   
      
         
            
                               {% for name,course,branch,image in comb_lis %}                

Here are the results:

               
                  Profile                   

Name: {{name}}

                  

Course: {{ course }},  {{ branch }} 

               
               {% endfor %}             
         
         
      
   

输出: