使用 django 将用户数据注册到 Firebase



我遇到了很大的麻烦。我创建了一个应用程序,管理员在其中创建用户并将详细信息发送到注册的电子邮件。但是现在我的客户希望将管理员创建的用户保存在Firebase中。我从来没有进入过火力基地,也做过研究,但找不到任何可以解决我问题的资源。

之前的代码是

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models.signals import post_save
from django.core.mail import EmailMessage
from django.utils import timezone
from django.dispatch import receiver
from django.db import models

class UserProfile(models.Model):
  user = models.ForeignKey(User,null=True)
  first_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the first name of user"))
  last_name = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the last name"))
  address = models.CharField(max_length=300,blank=True,null=True,help_text=("enter the address"))
  contact = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the contact"))
  email = models.EmailField(max_length=100,blank=True,null=True,help_text=("enter the email"))
  username = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the username"))
  password = models.CharField(max_length=100,blank=True,null=True,help_text=("enter the strong password"))
  creation_date = models.DateTimeField(editable=False,null=True)
  last_modified = models.DateTimeField(editable=False,null=True)
  def save(self, *args, **kwargs):
    if not self.creation_date:
      self.creation_date = timezone.now()
    self.last_modified = timezone.now()
    return super(UserProfile, self).save(*args, **kwargs)

  def __str__(self):
    return (self.username)

@receiver(post_save,sender=UserProfile)
def send_user_data_when_created_by_admin(sender, instance, **kwargs):
      first_name = instance.first_name
      print('first name is',first_name)
      last_name = instance.last_name
      address = instance.address
      print('address is',address)
      contact = instance.contact
      email = instance.email
      username = instance.username
      password = instance.password
      html_content = "your first name:%s <br> last name:%s <br> address:%s <br> contact:%s <br> email:%s <br> username:%s <br> password:%s"
      from_email = settings.DEFAULT_FROM_EMAIL
      message=EmailMessage('welcome',html_content %(first_name,last_name,address,contact,email,username,password),from_email,[email])
      message.content_subtype='html'
      message.send()

每当管理员从管理面板创建新用户时,如何将用户数据保存到 Firebase?

这是我将数据保存到Firebase的方式。

第一:

进口

from firebase import firebase

连接到用户数据库

db = firebase.FirebaseApplication("https://yourdb.firebaseio.com/users", None)

获取您的 Firebase 密钥并验证您的连接(密钥是您数据库的唯一密钥)(这只是一个假钥匙,仅供参考)

secret="adfkIDKDKkklff3777dlkdkLDLdd77RA"
auth=firebase.FirebaseAuthentication(secret,"","",True)
db.authentication = auth

以下我建议您在message.send()后在send_user_data_when_created_by_admin函数中执行此操作,因为您要保存的信息已经创建:

#let say the username is jonh45, the next line of code
#will save the value of the variable first_name in 
#https://yourdb.firebaseio.com/users/auth_info/jonh45/first_name
db.put("auth_info/"+username, "first_name", first_name)
#this will be saved to /users/auth_info/jonh45/email
db.put("auth_info/"+username, "email", email)

等等...

如果您需要任何进一步的帮助,请告诉我

最新更新