我正在使用 Flask 和 Firebase 创建一个简短的业务目录 Web 应用程序;我已经设法存储了收集的数据,但我也希望用户能够上传他们的商业照片;有了我所拥有的,当我存储照片时,firebase.storage()
我可以按预期创建一个文件夹和图像名称,但文件是空的。我一直在尝试不同的东西——比如我存储图片文件的位置,但它们都产生相同的结果——要么是空的图片文件,要么是错误消息。
我的目标是当用户注册他们的详细信息(以及上传他们的照片)时,他们可以登录他们将显示他们的详细信息和他们的商业图片的地方。
蟒蛇文件
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()
storage = firebase.storage()
@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)
registerList = db.child("Bame_Register").child("business").get(user['idToken']).val()
pic = storage.child("images/new.jpg").get_url(None)
return render_template('profile.html', registerList=registerList, pic=pic)
except:
return render_template('new.html', us=unsuccessful)
return render_template('new.html')
@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')
startYear = request.form.get('startYear')
businessDescription = request.form.get('businessDescription')
web = request.form.get('web')
businessNumber = request.form.get('businessNumber')
businessEmail = request.form.get('businessEmail')
businessImage = request.files.get('businessImage')
try:
user = auth.create_user_with_email_and_password(email, password)
auth.send_email_verification(user['idToken'])
bameRegister = dict(
userName = userName,
businessName = businessName,
businessStartYear = startYear,
businessDescription = businessDescription,
businessURL = web,
businessNumber = businessNumber,
businessEmail = businessEmail,
businessImage = businessImage,
)
storage.child("images/new.jpg").put(businessImage, user['idToken'])
results = db.child("Bame_Register").child("business").update(bameRegister, user['idToken'])
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><br>
<label for="startYear">Year buisness started:</label>
<input type="date" id="startYear" name="startYear" placeholder="2019"><br>
<textarea name="businessDescription" rows="8" cols="80" placeholder="Describe your business in a few sentences..." required></textarea><br>
<label for="web">Enter your business website (Optional):</label>
<input type="text" name="web" id="web" placeholder="Please start with https:// "><br>
<label for="businessNumber">Buisness number (Optional):</label>
<input type="number" name="businessNumber" id="businessNumber" placeholder="Enter landline or mobile number.."><br>
<label for="businessEmail">Buisness email (Optional):</label>
<input type="email" name="businessEmail" id="businessEmail" placeholder="Enter your business email"><br>
<label for="business_img">Select image:</label>
<input type="file" name="businessImage" accept="image/*">
<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>{{ registerList['businessName'] }}</title>
</head>
<body>
<h1>{{ registerList['businessName'] }}</h1>
<h2>{{ registerList['userName'] }}</h2>
<div class="">
<img src="{{ pic }}">
</div>
<hr>
<p>Year business started: {{ registerList['businessStartYear'] }}</p>
<p>{{ registerList['businessDescription']}}</p><br>
<p>{{ registerList['businessURL'] }}</p>
<p>{{ registerList['businessEmail'] }}</p>
<p>{{ registerList['businessNumber'] }}</p>
</body>
</html>
如果有人可以帮助我提出解决方案或向我指出我哪里出错了,我将不胜感激!
提前致谢 :)