在Django中从Firebase搜索数据
Firebase是谷歌的一个产品,它帮助开发者轻松地建立、管理和发展他们的应用程序。它帮助开发者以更快的速度和更安全的方式建立他们的应用程序。在Firebase方面不需要编程,这使得它很容易更有效地使用其功能。它提供云存储。它使用NoSQL来存储数据。
在这里,我们将学习如何在Firebase中搜索数据。要做到这一点,请按照以下步骤进行。
第1步:如果你是Firebase的新手,那么请参考这个。
第2步:进入urls.py文件,创建一个移动到网页上搜索数据的路径。
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),
]
步骤3 : 然后移动到views.py文件,编写以下函数来渲染成html页面。
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})
第四步:然后我们将移动到search.html页面,写下以下代码来搜索Firebase中的数据。为了更好地理解它,注释写在里面。
{% load static %}
<html lang="en">
<head>
<title>Search Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link rel='stylesheet' href="{% static '/css/Search.css' %}">
<link rel="stylesheet" type="text/css" href="{%static '/css/footer.css' %}">
</head>
<body>
<div class="container">
<div class="inner">
<form method="post" action="/searchusers/">
{% csrf_token %}
<!--Type the name you want to search and click on submit-->
<input type="text" placeholder="Enter Title..." aria-label="Search.." name="search"
id="search">
<select name="category" id="category" name="">
<option value="">Select Category</option>
<!--select type to user-->
<option value="Users">Users</option>
</select>
<input type="submit" value="Find">
</form>
</div>
</div>
</body>
</html>
第5步:然后我们将移动到searchusers.html页面,它将在网页上显示检索到的数据,如输出中所示。
{% load static %}
<html lang="en">
<head>
<title>User's List</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
<link rel='stylesheet' href="{% static '/css/Search.css' %}">
</head>
<body>
<div class="tm-container">
<div class="tm-main-content">
<section class="tm-section tm-section-small text-center">
<!--Showing all the details we retrieved Here-->
{% for name,course,branch,image in comb_lis %}
<h1>Here are the results:</h1>
<div class="image">
<img src="{{image}}" alt="Profile">
<h3 class="tm-section-header3">Name: {{name}}</h3>
<h3 class="tm-section-header2">Course: {{ course }}, {{ branch }} </h3>
</div>
{% endfor %}
</section>
</div>
<br>
</div>
</body>
</html>