Press ESC to close

201 16

Python Django with Google Firebase Part 1

Looking for an easy way to add a database to your Python Web App? We’ll show you how to get started with Firebase integration in Python Django. Creating an app isn’t easy, but learning how to store your app’s vital data is! Here is a step-by-step tutorial to help you figure out how to connect to a Python Django Web App with Firebase and take the headache out of user authentication, data storage and retrieval. A Python app without Firebase is like a chicken without a hen-house; if you don’t have a place to store your eggs, it’s time to re-think the farm.

In this beginners tutorials we’ll work on Connecting Firebase with Django and creating a Simple SignIn form to showcase the example of Firebase authentication.

Check out this video, code below follows the video to help:


If you like videos like this consider donating $1, or simply turn off AdBlocker. Either helps me to continue making tutorials.


 
Transcript / Cheat Sheet

1. Configure FIREBASE 

 
Create new project by Selecting Add Project.
 
Navigate to Authentication on Dashboard and set SIGN-IN method Email/Password to enable.
 
For first time add a manual user.
 
Now, navigate to Project Overview and select Add Firebase to WebApp now, copy config’s for later use in Django.
Now Let’s Get started for integrating Firebase with Python Django .

2. Installing Required Libraries

Installing pip

Before we can connect to our Firebase database with Python, we need to download a couple of helper modules. We are going to use pip to install them.
You can check to see if you currently have pip install by running the command in the terminal:
pip3 --version
If it is not currently installed, you can install pip by running following command:
sudo apt-get -y install python3-pip && sudo apt-get update

 Installing Pyrebase

Pyrebase is a Python interface to Firebase’s REST API. In layman’s terms, it allows you to use Python to manipulate your Firebase database. The documentation for Pyrebase can be found at https://github.com/thisbejim/Pyrebase
Python Django with Google Firebase

We will install the Pyrebase and its dependencies using pip:

sudo pip3 install Pyrebase

 Installing Django 

sudo pip3 install django

3. Working with Django

Creating new Django Project :

# sudo django-admin startproject cpanel 

# cd cpanel

# python3 manage.py runserver
//Open browser & type : 127.0.0.1:8000
//Output : IT'S WORKED
//now make migrations:
# python3 manage.py migrate
// create a directory named templates
# mkdir templates
// go to cpanel directory and create views.py file
# cd cpanel
# touch views.py 
// go to settings.py file and configure the templates directory over there
// go to line 57 and edit it:
        'DIRS': ['templates'],

Code for Views.py :































import pyrebase from django.shortcuts import render
config = {
apiKey: AIzaS–your config here —fOMd4pYUkbkZVvI,
authDomain: cpanel-5e873.firebaseapp.com,
databaseURL: https://cpanel-5e873.firebaseio.com,
projectId: cpanel-5e873,
storageBucket: cpanel-5e873.appspot.com,
messagingSenderId: 579985583952
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
def singIn(request):
return render(request, signIn.html)
def postsign(request):
email=request.POST.get(email)
passw = request.POST.get(pass)
try:
user = auth.sign_in_with_email_and_password(email,passw)
except:
message = invalid cerediantials
return render(request,signIn.html,{msg:message})
print(user)
return render(request, welcome.html,{e:email})

Code for Urls.py :









from django.contrib import admin
from django.conf.urls import url
from . import views
urlpatterns = [
url(r^admin/, admin.site.urls),
url(r^$,views.singIn),
url(r^postsign/,views.postsign),
]

Code for Html Templates :

SignIn.html
{% if msg %}
<script>
alert({{msg}})
</script>
{% endif %}
<html lang=en>
<head>
<meta charset=UTF-8>
<title>Sign</title>
</head>
<body>
<form action=/postsign/ method=post>
{% csrf_token %}
Email :
<input type=email name=email>
Password:
<input type=password name=pass>
<input type=submit value=SignIn>
</form>
</body>
</html>

Welcome.html

<html lang=en>
<head>
<meta charset=UTF-8>
<title>Title</title>
</head>
<body>
{% csrf_token %}
Welcome {{e}}
</body>
</html>

That’s end up with great on Tutorial on Python Django with Firebase : Getting Started and Firebase Authentication, I hope you guys enjoyed it 🙂

Get full source code for Django Project Here.

16 Comments

  1. Unknown on September 10, 2018

    django-admin.py startproject starts pycharm or visual studio IDE rather than creating project. Help me please

  2. Unknown on October 18, 2018

    what about hosting django projects on firebase?
    Because the firebase cli redirects everything to the public folder which firebase serves..
    I was looking for some tutorial about it and found nothing !!

  3. Anonymous on November 3, 2018

    How to save a user on the db?

  4. Unknown on November 3, 2018

    FIrebase DB

  5. kavin sharma on November 3, 2018

    sorry to say, but you can't host django on firebase hosting. For that you can use google cloud console check out this blog : https://hackanons.com/2018/10/python-django-webser-hosting-on-google.html

  6. Unknown on January 3, 2019

    oh my very useful and easy article , thanks a ton

  7. jorick228 on January 30, 2019

    Make money on kasino online and do not regret anything gamble casino with us Make money on kaino online and do not regret anything.

  8. ESHAN CHATTARAJ on March 12, 2019

    good concept

  9. MUGOYA DIHFAHSIH on April 2, 2019

    Thanks for documenting Django-Pyrebase-firebase

  10. Praveenkumar on July 3, 2019

    i got an error it says
    https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=AIzaS–your%20config%20here%20—fOMd4pYUkbkZVvI%5D {
    "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "errors": [
    {
    "message": "API key not valid. Please pass a valid API key.",
    "domain": "global",
    "reason": "badRequest"
    }
    ],
    "status": "INVALID_ARGUMENT"
    }
    }

    please help

  11. Unknown on October 28, 2019

    Great article, thanks.

  12. Manuel Pizarro on January 16, 2020

    def postsign(request):
    email = request.POST.get('email')
    passw = request.POST.get("pass")
    message = "Login succesfull"

    Se debe definir message antes de el except

  13. #mansab ch on April 23, 2020

    Al-Zahra area, which is one of the largest regions in the State of Kuwait, and we at Kuwait فني تكييف company are happy to offer repair services for Al-Zahraa Air Conditioning to all of our customers in this region

  14. Unknown on May 3, 2020

    فني تكييف الكويت تقدم لكم جميع خدمات فني تكييف ٢٤ ساعه و تصليح مكيفات و فني تكييف مركزي هندي
    ، صيانة تكييف مركزي و تصليح تكييف مركزي و تصليح وحدات تكييف
    تركيب تكييف و كمبروسرات جديد و مستامل جميع مناطق بالكويت فني تكييف

  15. google porn porn google on September 10, 2020

    google porn porn google

Leave a Reply