How to push data from html form to firebase realtime database in django(using pyrebase)?
I'm having a problem with pushing data from a form to firebase realtime database. Here is my view funcction. Just updated to include user token but hasn't helped. The data isn't displaying in the db.
def post_add(request):
name = request.POST.get('name')
phone = request.POST.get('phone')
location = request.POST.get('location')
city = request.POST.get('city')
website = request.POST.get('website')
data = {"name": name, "phone": phone, "location": location, "city": city, "website": website}
user = authe.get_account_info(request.session['uid'])
db.child("restaurant").push(data,user['users'][0]['localId'])
return render(request, 'restaurants.html')
Just updated to include user token but hasn't helped. The data isn't displaying in the db.
I think you should be using set() or update() function.
Check the docs here
const postData = {
author: username,
uid: uid,
body: body,
title: title,
starCount: 0,
authorPic: picture
};
// Write the new post's data simultaneously in the posts list and the user's post list.
const updates = {};
updates['/posts/' + newPostKey] = postData;
updates['/user-posts/' + uid + '/' + newPostKey] = postData;
return update(ref(db), updates);
This is a Javascript snippet. But it would be mostly same for Python firebase SDK.
For your database the code should look something like this :
db.child("restaurant").update(data,user['users'][0]['localId'])
Try and let me know. This should work.
Be careful, set will replace the entire JSON tree for the given key, update won't.
ps : But ideally, you should structure your data so that keys are predictable and unique.