Firebase es un producto de Google que ayuda a los desarrolladores a crear, administrar y hacer crecer sus aplicaciones fácilmente. Ayuda a los desarrolladores a crear sus aplicaciones de forma más rápida y segura. No se requiere programación en el lado de firebase, lo que facilita el uso de sus funciones de manera más eficiente. Proporciona almacenamiento en la nube. Utiliza NoSQL para el almacenamiento de datos.
Aquí, vamos a aprender cómo podemos buscar datos en Firebase. Para hacerlo, siga los siguientes pasos:
Paso 1: si es nuevo en firebase, consulte este .
Paso 2: Vaya al archivo urls.py y cree una ruta para moverse a la página web para buscar datos.
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), ]
Paso 3: Luego muévase al archivo views.py y escriba la siguiente función para representar en la página 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})
Paso 4: Luego nos moveremos a la página search.html y escribiremos el siguiente código para buscar datos en firebase. Los comentarios están escritos dentro para entenderlo mejor.
HTML
{% 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>
Paso 5: luego nos moveremos a la página searchusers.html y mostrará los datos recuperados en la página web como se muestra en la salida.
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>
Producción: