TypeError: 'Session' object does not support item assignment
TypeError: 'Session' object does not support item assignment
我正在使用 Flask-Session 来确定是否有人登录了网站,但是当我尝试为其分配一个值时,它 returns 出现错误 "TypeError: 'Session' object does not support item assignment"。
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
username = request.form["gets username input"]
password = request.form["gets password input"]
allowed = db.execute(SQL for checking if the username and password are in the SQL db)
allowed = allowed.first()[0]
if allowed == 1:
loginsession = Session()
id = db.execute(SQL for finding the id of the user)
loginsession["id"] = id
print(loginsession["id"])
return redirect(url_for("the route to the profile page"))
else:
return "Incorrect username or password"
else:
return render_template("login.html")
感谢您的帮助!
你可以使用这个例子
@app.route("/login",methods = ["POST","GET"])
def login():
if request.method == "POST":
try:
Email = request.form["email"]
pwd = request.form["pwd"]
with sqlite3.connect("Account.db") as con:
cur = con.cursor()
print("Connection test")
cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
row = cur.fetchone()
print("query test")
while row is not None:
session['email']=request.form['email']
print(row[1])
return render_template("success.html",msg = msg)
else:
msg = "sorry wrong id"
return render_template("failure.html",msg = msg)
except:
con.rollback()
msg = "problem"
然后在另一个函数中
if 'email' in session:
email = session['email']
return render_template("view.html")
else:
return '<p>Please login first</p>'
我正在使用 Flask-Session 来确定是否有人登录了网站,但是当我尝试为其分配一个值时,它 returns 出现错误 "TypeError: 'Session' object does not support item assignment"。
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
username = request.form["gets username input"]
password = request.form["gets password input"]
allowed = db.execute(SQL for checking if the username and password are in the SQL db)
allowed = allowed.first()[0]
if allowed == 1:
loginsession = Session()
id = db.execute(SQL for finding the id of the user)
loginsession["id"] = id
print(loginsession["id"])
return redirect(url_for("the route to the profile page"))
else:
return "Incorrect username or password"
else:
return render_template("login.html")
感谢您的帮助!
你可以使用这个例子
@app.route("/login",methods = ["POST","GET"])
def login():
if request.method == "POST":
try:
Email = request.form["email"]
pwd = request.form["pwd"]
with sqlite3.connect("Account.db") as con:
cur = con.cursor()
print("Connection test")
cur.execute("SELECT * FROM Account WHERE Email= ? and Password= ?",(Email, pwd))
row = cur.fetchone()
print("query test")
while row is not None:
session['email']=request.form['email']
print(row[1])
return render_template("success.html",msg = msg)
else:
msg = "sorry wrong id"
return render_template("failure.html",msg = msg)
except:
con.rollback()
msg = "problem"
然后在另一个函数中
if 'email' in session:
email = session['email']
return render_template("view.html")
else:
return '<p>Please login first</p>'