我正在使用 Firebase、Pyrebase 和 Flask 构建一个快速注册页面;我设法创建了一个用户帐户并将数据推送到业务文件夹;我还创建了一个登录名,用户登录到个人资料页面 - 显示他们的姓名和他们的公司名称。
我唯一的问题是我无法提取单个数据,而是将数据显示为单个块;这让我很难找到一种businessName
仅从userName
.registerList
我还希望只显示已登录的用户的信息 - 我想我需要使用某种user[idToken]
我不确定的类型。相反,我得到了整个列表(我知道使用 for 循环没有帮助!)
注册.py
import pyrebase
from flask import *
app = Flask(__name__)
config = {
"apiKey": "",
"authDomain": "",
"databaseURL": "",
"projectId": "",
"storageBucket": "",
"messagingSenderId": "",
"appId": "",
"measurementId": ""
}
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
db = firebase.database()
@app.route('/', methods=['GET', 'POST'])
def login():
unsuccessful = 'Please check your credentials'
successful = 'Login successful'
if request.method == 'POST':
email = request.form.get('name')
password = request.form.get('pass')
try:
user = auth.sign_in_with_email_and_password(email, password)
register = db.child("Bame_Register").child("business").get()
registerList = register.val()
return render_template('profile.html', registerList=registerList)
except:
return render_template('new.html', us=unsuccessful)
return render_template('new.html', us=unsuccessful)
@app.route('/register', methods=['GET', 'POST'])
def register():
unsuccessful = 'Please check your credentials'
successful = 'Registraion successful'
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('pass')
userName = request.form.get('inputName')
businessName = request.form.get('businessName')
try:
user = auth.create_user_with_email_and_password(email, password)
auth.send_email_verification(user['idToken'])
bameRegister = dict(userName=userName, businessName=businessName)
db.child("Bame_Register").child("business").push(bameRegister)
return render_template('new.html', x=successful)
except:
return render_template('new.html', y=unsuccessful)
return render_template('new.html')
if __name__ == '__main__':
app.run(debug=True)
新的.html
<!DOCTYPE html>
<html>
<head>
<title>Flask Firebase Auth</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
{% if s %}
<div class="alert alert-success">
<h2>{{s}}</h2>
</div>
{% endif %}
{% if us %}
<div class="alert alert-danger">
<h2>{{us}}</h2>
</div>
{% endif %}
<form action="/" method="post">
<h2>Please sign in</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="name" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<br>
<div class="container">
{% if x %}
<div class="alert alert-success">
<h2>{{x}}</h2>
</div>
{% endif %}
{% if y %}
<div class="alert alert-danger">
<h2>{{y}}</h2>
</div>
{% endif %}
<form action="{{ url_for('register') }}" method="post">
<h2>Please create an account to register your business</h2>
<label for="inputEmail">Email address</label>
<input type="email" id="inputEmail" name="email" placeholder="Email address" required autofocus>
<label for="inputPassword">Password</label>
<input type="password" id="inputPassword" name="pass" placeholder="Password" required>
<br>
<label for="inputName">Enter your name: </label>
<input type="text" id="inputName" name="inputName" placeholder="Name" required>
<label for="businessName">Business Name</label>
<input type="text" id="businessName" name="businessName" placeholder="Business Name" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Register business</button>
</form>
</div>
</body>
</html>
profile.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<title>dynamic pages</title>
</head>
<body>
{% for key,value in registerList.items() %}
{% for data in value.values() %}
<h4>{{ data }}</h4>
{% endfor %}
{% endfor %}
<h6>Dynamic profile page</h6>
</body>
</html>
我将如何只显示用户的姓名或仅显示他们的公司名称?我在想也许data[userName]
或者data[businessName]
,但那没有用!我希望能够单独获取数据,以便控制数据的显示位置。显示的数据应该只属于已登录的用户。然后我可以稍后添加用户可以编辑其信息的功能。