Header Ads

Python Django With Google Firebase : Firebase Database Push Data Using Form #Pyrebase #Part 4


In this part of Python Django with Google Firebase series we'll cover Firebase Database i.e real-time database as Django backend using Pyrebase library. For showcasing the implementation we'll use a HTML Form created using Django templates to Push data to Firebase Database, later i will show you how we retrieve data from Firebase to Django template.



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 :

Templates:

First we need to edit our 'welcome.html' on which we'll add button for CREATE REPORT & CHECK REPORT .

Welcome.html
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
div{
position : absolute;
right : 10px;
top : 5px
}
</style>
</head>
<body>
<div>
<button type="button" onclick="location.href='{% url 'log' %}'">Logout</button>
</div>
Welcome {{e}}
<br><br>
<button type="button" onclick="location.href='{% url 'create' %}'">Create Report</button>
<button type="button">Check Report</button>
</body>
</html>
Create.html
Now create a simple HTML Form for pushing data to Firebase database, we'll be redirected to this form by clicking on previously created CREATE REPORT button
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign</title>
<style>
div{
position : absolute;
right : 10px;
top : 5px
}
</style>
</head>
<body>
<div>
<button type="button" onclick="location.href='{% url 'log' %}'">Logout</button>
</div>
<H2>Create Daily Progress Report</H2>
<form action="/post_create/" method="post">
{% csrf_token %}
Work Assigned :
<input type="text" name="work" required><br><br>
Progress:
<textarea rows="5" cols="40" name="progress" required></textarea>
<br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

Source code for SignIn.html & SignUp.html 

Views.py
import pyrebase from django.shortcuts import render
from django.contrib import auth
config = {
'apiKey': "AIzaSyB0Il0NLQPxxDyMgoE0fOMd4pYUkbkZVvI",
'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)
authe = firebase.auth()
database=firebase.database()
def signIn(request):
return render(request, "signIn.html")
def postsign(request):
email=request.POST.get('email')
passw = request.POST.get("pass")
try:
user = authe.sign_in_with_email_and_password(email,passw)
except:
message="invalid credentials"
return render(request,"signIn.html",{"messg":message})
print(user['idToken'])
session_id=user['idToken']
request.session['uid']=str(session_id)
return render(request, "welcome.html",{"e":email})
def logout(request):
auth.logout(request)
return render(request,'signIn.html')
def signUp(request):
return render(request,"signup.html")
def postsignup(request):
name=request.POST.get('name')
email=request.POST.get('email')
passw=request.POST.get('pass')
try:
user=authe.create_user_with_email_and_password(email,passw)
except:
message="Unable to create account try again"
return render(request,"signup.html",{"messg":message})
uid = user['localId']
data={"name":name,"status":"1"}
database.child("users").child(uid).child("details").set(data)
return render(request,"signIn.html")
def create(request):
return render(request,'create.html')
def post_create(request):
import time
from datetime import datetime, timezone
import pytz
tz= pytz.timezone('Asia/Kolkata')
time_now= datetime.now(timezone.utc).astimezone(tz)
millis = int(time.mktime(time_now.timetuple()))
print("mili"+str(millis))
work = request.POST.get('work')
progress =request.POST.get('progress')
idtoken= request.session['uid']
a = authe.get_account_info(idtoken)
a = a['users']
a = a[0]
a = a['localId']
print("info"+str(a))
data = {
"work":work,
'progress':progress
}
database.child('users').child(a).child('reports').child(millis).set(data)
name = database.child('users').child(a).child('details').child('name').get().val()
return render(request,'welcome.html', {'e':name})

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.signIn),
url(r'^postsign/',views.postsign),
url(r'^logout/',views.logout,name="log"),
url(r'^signup/',views.signUp,name='signup'),
url(r'^postsignup/',views.postsignup,name='postsignup'),
url(r'^create/',views.create,name='create'),
url(r'^post_create/',views.post_create,name='post_create')
]
 That's end up Python Django with Firebase Database Push Data for more functionality refer Pyrebase library page. In next tutorial we will cover Python django with Firebase database Retrieve Data to Django Template  if you guys facing some issue comment out :)  

Whole Project Source Code here   

No comments:

Powered by Blogger.