Header Ads

Python Django with Google Firebase : Getting Started, Intro, Basic Configuration & Firebase Authentication #Part1

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 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 WebApp 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 

Open your Firebase Console


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
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.



No comments:

Powered by Blogger.