3

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fariha
  • 49
  • 1
  • 1
  • 5

2 Answers2

2

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")
joke4me
  • 812
  • 1
  • 10
  • 29
0

You can try below.

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