[Django]-How to implement FirebaseDB with a Django Web Application

13👍

take a look at the database section of the doc. It details how you would interact with the database API. Typically you would use these in your views where you need to save or retrieve something from the database.

e.g say you want a view that gets users, you could have something like this:

#views.py
from pyrebase_settings import db, auth
from django.shortcuts import render

def get_users(request):
    users = db.child("users").get()
    return render(request, 'users.html', {'users': users.val()})

The documentation for retrieving data can be seen here

also say you want a view to save(signup) users, you could have something like so:

#views.py
from pyrebase_settings import db, auth

def signup(request):
   form = SignupForm(request.POST)
   if form.is_valid():
       email = form.cleaned_data('email')
       password = form.cleaned_data('password')
       auth.create_user_with_email_and_password(email, password)
   # the rest of your code goes here

the create_user_with_email_and_password method is documented here

PS. I have never made use of the pyrebase library so I write only based on what is specified in its docs. Also I am only writing these django snippets off the top of my head so forgive me if there are any syntax errors or typos 🙂

Hope this helps 🙂

8👍

Is there something else I need to understand about Firebase?

No, there is something you need to understand about Django — it is designed to use a relational database engine as its primary data store. Firebase is not relational.

Sorry to be the bearer of bad news.

Many answers on this question are missing this point, and suggesting code or libraries that let you access Firebase from Python. Sure, that’s possible. But that doesn’t make it work as a primary data store for Django. Django’s ORM features and contrib apps all presume a relational database.

In order for this to work you’d need something along the lines of django-nonrel that supported Firebase. As far as I know that doesn’t exist.

3👍

consider having a look at the fcm-django library here https://github.com/xtrinch/fcm-django

0👍

My solution for getting multiple files from html and upload that files in firebase storage in web django

index.html

<div id="fine-uploader-manual-trigger">
<input class="custom-file" type="file" name="imgname" id="productImgUpload" accept=".xlsx,.xls,image/*,.doc,audio/*,.docx,video/*,.ppt,.pptx,.txt,.pdf" multiple>
</div> 

views.py

from firebase import Firebase

def storeimg(request):
  config = {
            "apiKey": "",
            "authDomain": "",
            "databaseURL": "",
            "storageBucket": ""
        }
  firebase = Firebase(config)
  storage = firebase.storage()
  finalimglist = []
  imglist = []
  for i in range(len(filepath)):
       storage.child("Product/"+cname+"/"+str(filepath[i])).put(filepath[i])
       imgurl = storage.child("Product/"+cname+"/"+str(filepath[i])).get_url(token='null')
       imglist.append(imgurl)

  finalimglist.append(imglist)

-1👍

Pyrebase is a wrapper around the Firebase for python. I don’t recommend using Pyrebase because it lags support for Firestore and FCM. When everything in Django is done on Serverside, why not use the Firebase Admin SDK on the Django server. You can also manage users with it. I mean there is good documentation for adding the Firebase to our server. Also, they have docs in Python, NodeJS etc., for the server. I know its kind of going towards API stuff. But I feel its the right way to do in Django since Django is a server application. Check out this link

Leave a comment