Skip to content
Related Articles

Related Articles

Improve Article

How to create a new project in Django using Firebase Database?

  • Difficulty Level : Medium
  • Last Updated : 30 Jun, 2021

Django is a Python-based web framework that allows you to quickly create efficient web applications. If you are new to Django then you can refer to Django Introduction and Installation. Here we are going to learn How to create a Django project using Firebase as Database . 

How to create a new project in Firebase ?

Step 1: Firstly, We are going to Create a project on Firebase to connect our static web page. Visit Firebase Page For Configuring Your Project – https://console.firebase.google.com/u/0/?pli=1

Click On Add Project Button.

Step 2: Give a Name To your Project and Click On Continue Button.



 Step 3: Now Click On Continue Button.

Step 4: Now Choose Default Account For Firebase and Click On Create Project.

Step 5: Now Your Project is created. You are Good to Go. Click on Continue .

Step 6:  Now Click On 3rd icon that’s Web Button(</>).



Step 7: Give a nickname  to your Web Project and Click On Register App

Step 8: Now you will see the configuration of your App like this. Copy this Code somewhere .You wlll need it later.

Step 9 : Click On The Realtime Database button As Shown In Figure. 

Step 10: Now Click On Create Database.

Step 11: Now Click On Test Mode and then Click On Enable.



Now, We will Add Some Data and will try to Retrieve that using our Website

Integrating Firebase Database to Django Project – 

Now, I hope that you have already create a project in Django. If not then Refer to How to Create a Basic Project using MVT in Django ? Since we are using firebase as Database , We need to install pyrebase . For this Type the following Command in terminal

$pip install pyrebase4

Create a views.py file in your project directly. The Structure should be like this .

Urls.py file

Python3




from django.contrib import admin
from django.urls import path
from . import views
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home),
]

Views.py

Python3






from django.shortcuts import render
import pyrebase
 
config={
    apiKey: "Use Your Api Key Here",
    authDomain: "Use Your authDomain Here",
    databaseURL: "Use Your databaseURL Here",
    projectId: "Use Your projectId Here",
    storageBucket: "Use Your storageBucket Here",
    messagingSenderId: "Use Your messagingSenderId Here",
    appId: "Use Your appId Here"
}
firebase=pyrebase.initialize_app(config)
authe = firebase.auth()
database=firebase.database()
 
def home(request):
    day = database.child('Data').child('Day').get().val()
    id = database.child('Data').child('Id').get().val()
    projectname = database.child('Data').child('Projectname').get().val()
    return render(request,"Home.html",{"day":day,"id":id,"projectname":projectname })

 

 

Home.html

 

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sample Project</title>
</head>
<body>
<H1>
Project Name is {{ projectname }}
    </H1>
<br/>
<h2>
Project Id is {{ id }}
    </h2>
<br>
<h3>
Day {{ day }}
    </h3>
<br>
</body>
</html>

 

 

Now move to your project directory and run our project using the given command :

 

python manage.py runserver

 

Project Output will be as Follow – 

 

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :