Fariha
Fariha

Reputation: 49

How to check if a child in firebase database exists using Django?

the following code throws attribute error

''Database' object has no attribute 'exists''.

    from django.shortcuts import render
    from django.views import View
    from django.views.decorators.cache import cache_page
    from django.views.decorators.csrf import csrf_protect
    import pyrebase
    from django.contrib import auth
    import json
    import requests
    from . import services
    from .models import Product


    authe = services.firebase_key().auth()
    database = services.firebase_key().database()

    def Login(request):

        return render(request, "Login.html")


    def postsign(request):

         data = services.get_products()
         print(data)

         context = {'data': data}
         number = request.POST.get('number')
         password = request.POST.get("password")




         if database.child("users").child(number).exists():
             user = database.child("users").child(number).get().val()
             if user['number'] == number:
               if user['password'] == password:

                 return render(request,"Welcome.html",context)

I need to check if the number exists in the database or not since I want the existing users to log in using numbers and passwords.

Upvotes: 3

Views: 1421

Answers (2)

BK94
BK94

Reputation: 79

You can try below.

if database.child('users').child(number).shallow().get().val():
    print("user exists")
else:
    print("user does not exist")

Upvotes: 0

joke4me
joke4me

Reputation: 842

Check if child exist in Python using Pyrebase, should be something similar, see official docs here

if not database.child('users').shallow().get().val():
    print("users does not exist")
else:
    print("users exist")

Upvotes: 2

Related Questions