trying to make a simple sign-in website with flask, came across this error TypeError: 'SecureCookieSession' object is not callable and dont know any solution. Help wanted. error message `
from flask import Flask, render_template, redirect, request, session
import mysql.connector
import gunicorn
app = Flask(__name__)
app.secret_key = "coolguy"
app.config.update(
SESSION_COOKIE_SECURE=True,
DEBUG=True,
REMEMBER_COOKIE_SECURE=True,
)
my_db = mysql.connector.connect(
host="localhost",
user="root",
password="wagner2020",
database="seconddb"
)
mycursor = my_db.cursor()
mycursor.execute(
"CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(255), password VARCHAR(255))")
@app.route("/")
def home():
if "username" in session:
list = ''
return render_template("index.html", user=session("username"), list=list)
else:
return render_template("login.html")
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
sql = "SELECT username FROM users WHERE username = %s AND password = %s"
values = [username, password]
mycursor.execute(sql, values)
myresult = mycursor.fetchall()
if len(myresult) > 0:
session["username"] = username
return redirect("/")
else:
return render_template("login.html", message="Wrong username or password.")
else:
return render_template("login.html")
mycursor.execute(sql, value)
myresult = mycursor.fetchall()
@app.route("/register", methods=["POST", "GET"])
def signup():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirm_password = request.form.get("confirm-password")
if (password != confirm_password):
return render_template("register.html", message="passwords don't match")
sql = "SELECT username FROM users WHERE username = %s AND password = %s"
value = (username, password)
mycursor.execute(sql, value)
myresult = mycursor.fetchall()
if len(myresult) > 0:
return render_template("register.html", message="Username already taken. Try something new!")
else:
sql = "INSERT INTO users(username, password) VALUES (%s, %s)"
values = [username, password]
mycursor.execute(sql, values)
my_db.commit()
session["username"] = username
return redirect("/")
else:
return render_template("register.html")
@app.route('/logout')
def logout():
session.pop("username", None)
return render_template("login.html")
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)`
If you know the solution that'd be great as i cant figure out why secure cookie session would be uncallable in this situation.
CodePudding user response:
[I] cant figure out why secure cookie session would be uncallable in this situation.
Why do you think that session object is callable? In the Flask API for session object it is mentioned that session object acts like an ordinary dictionary. As much as a dictionary is not callable, session is not callable either.
And that leads us to the line in your code that caused the error. That is:
return render_template("index.html", user=session("username"), list=list)
Here, session("username")
is written as if session is callable. Correct the syntax to session.get('username')
or session['username']
.