使用字典数据库结构在 Flask 应用程序中进行身份验证
Authentication in Flask application using dictionary database structure
我正在创建一个基本应用程序来演示使用会话在 Flask 中进行注册和登录活动。对于下面的代码,字典中可用的每个用户都应该能够登录。但是,该应用程序只接受名为 'Mary' 的第一个用户的登录。不明白哪里错了
from flask import Flask, app, session, render_template, request,redirect, url_for, jsonify
app = Flask(__name__)
app.config['SECRET_KEY'] = "Zd9TUg1aE03bHc28"
@app.route('/')
def load():
return render_template("authusers.html")
class Mydatabase:
appdb=[{'username':'Mary','password':'Password@123'},
{'username':'John','password':'Password@456'},
{'username':'Tara','password':'Password@789'}]
mydbusers=Mydatabase()
@app.route('/login',methods = ['GET','POST'])
def success():
if request.method == "POST":
username_val = request.form['user_id']
userpassword_val = request.form['user_password']
for authuser in Mydatabase.appdb:
for authpassword in authuser.values():
if authuser['username'] == username_val and authpassword['password'] == userpassword_val:
session['reg_user'] = username_val
return f'{session.get("reg_user")} have successfully logged into the application';
else:
return redirect(url_for('register'))
@app.route('/logout', methods=['GET','POST'])
def logout():
if 'reg_user' in session:
session.pop('reg_user',None)
return render_template("authusers.html")
@app.route('/register', methods=['GET','POST'])
def register():
return render_template('register.html')
@app.route('/reg_success',methods=['GET','POST'])
def reg_success():
newusercred={'username':request.form['user_id'], 'password':request.form['user_password']}
mydbusers.appdb.append(newusercred)
# return jsonify(Mydatabase.appdb)
return render_template("authusers.html")
if __name__=="__main__":
app.run(debug=True)
我在你的代码中发现了一些逻辑问题
这样试试 ->
class Mydatabase:
appdb=[{'username':'Mary','password':'Password@123'},
{'username':'John','password':'Password@456'},
{'username':'Tara','password':'Password@789'}]
username_val = 'Tara'
userpassword_val = 'Password@789'
d = [a for a in Mydatabase.appdb if a["username"] == username_val and a["password"]==userpassword_val]
if d:
print(f'have successfully logged into the application')
else:
print(f'Wrong credentials')
你的代码中有一些不需要的循环,你 return 即使在 else
部分。
我正在创建一个基本应用程序来演示使用会话在 Flask 中进行注册和登录活动。对于下面的代码,字典中可用的每个用户都应该能够登录。但是,该应用程序只接受名为 'Mary' 的第一个用户的登录。不明白哪里错了
from flask import Flask, app, session, render_template, request,redirect, url_for, jsonify
app = Flask(__name__)
app.config['SECRET_KEY'] = "Zd9TUg1aE03bHc28"
@app.route('/')
def load():
return render_template("authusers.html")
class Mydatabase:
appdb=[{'username':'Mary','password':'Password@123'},
{'username':'John','password':'Password@456'},
{'username':'Tara','password':'Password@789'}]
mydbusers=Mydatabase()
@app.route('/login',methods = ['GET','POST'])
def success():
if request.method == "POST":
username_val = request.form['user_id']
userpassword_val = request.form['user_password']
for authuser in Mydatabase.appdb:
for authpassword in authuser.values():
if authuser['username'] == username_val and authpassword['password'] == userpassword_val:
session['reg_user'] = username_val
return f'{session.get("reg_user")} have successfully logged into the application';
else:
return redirect(url_for('register'))
@app.route('/logout', methods=['GET','POST'])
def logout():
if 'reg_user' in session:
session.pop('reg_user',None)
return render_template("authusers.html")
@app.route('/register', methods=['GET','POST'])
def register():
return render_template('register.html')
@app.route('/reg_success',methods=['GET','POST'])
def reg_success():
newusercred={'username':request.form['user_id'], 'password':request.form['user_password']}
mydbusers.appdb.append(newusercred)
# return jsonify(Mydatabase.appdb)
return render_template("authusers.html")
if __name__=="__main__":
app.run(debug=True)
我在你的代码中发现了一些逻辑问题
这样试试 ->
class Mydatabase:
appdb=[{'username':'Mary','password':'Password@123'},
{'username':'John','password':'Password@456'},
{'username':'Tara','password':'Password@789'}]
username_val = 'Tara'
userpassword_val = 'Password@789'
d = [a for a in Mydatabase.appdb if a["username"] == username_val and a["password"]==userpassword_val]
if d:
print(f'have successfully logged into the application')
else:
print(f'Wrong credentials')
你的代码中有一些不需要的循环,你 return 即使在 else
部分。