Skip to content
Sign UpLog In
This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs

Help with Flask session


I'm using the standard session import from Flask, and I use sessions to store data! I use it for the login page, where it works perfectly fine, however when I use it for my Profile page, it doesn't retain the data set on the session. Here's my code:

@app.route('/login', methods = ['GET', 'POST']) def login(): if "Username" in session: return redirect("/") if request.method == 'POST': req = request.form global username username = req.get('username').strip() password = req.get('password') if username in list(db.keys()): if db[username][0] == password: if (db[username][7])[2] == "True": if recaptcha.verify(): session['Username'] = username session.permanent = True username = session["Username"] return redirect('/') else: return render_template('login.html', amends = "Recaptcha failed!") else: return render_template('login.html', amends = "The account hasn't been verified yet.") else: return render_template('login.html', amends = "The username or password is invalid.") else: return render_template('login.html', amends = "The username or password is invalid.") return render_template('login.html')

However, when I use it for the profile page, it doesn't retain:

@app.route('/profile', methods = ['GET', 'POST']) def profile(): #addRef() username = session["Username"] olist = db[username][7] if request.method == "POST": #print(session['apptheme']) newlist = [] req = request.form determine = req.get("determine") appearance = "" appearance = req.get("appearance").strip() #session.permanent = True print("o.o.o.o.o.o") stat = req.get("stat") if determine == "yes": newlist.append("True") elif determine == "no": newlist.append("False") newlist.append(stat) newlist.append(olist[2]) info = [db[username][0],db[username][1],db[username][2],db[username][3],db[username][4],db[username][5],db[username][6],newlist] db[username] = info session['apptheme'] = appearance session.permanent = True appear = session["apptheme"] return redirect('/profile') status = db[username][4] listpref = db[username][7] oldstat = listpref[1] ident = "" tag = "" avurl = "" if username in adminList: a = "🔵 " else: a = "" if db[username][6] == "": des = False else: des = True if db[username][6] != "": try: discord = OAuth2Session(client_id, token=db[username][6]) response = discord.get(base_discord_api_url + '/users/@me') ident = response.json()['id'] av1 = response.json()['avatar'] avurl = "" + ident + "/" + av1 tag = response.json()['username'] + "#" + response.json()['discriminator'] except Exception as e: print("Error in Logging in with Discord!") des = False ang = "Sync" if 'apptheme' in session: ang = session['apptheme'] else: ang = "Sync" return render_template('profile.html', status=status,email=db[username][3],username=username, prefix=a, avatar=avurl, tag=tag, des=des, oldstat=oldstat, av=av(session["Username"]), ident=ident, ang=ang)

Here's my login.html code:

<html> <head><title>Login</title> </head> <body id="page-top"> <div id="wrapper"> {% if session["Username"]: %} {% include 'nav.html' %} {% else %} {% include 'notnav.html' %} {% endif %} <div class="container-fluid"> <div class="container"> <div class="row justify-content-center"> <div class="col-xl-10 col-lg-12 col-md-9"> <center> <div class="card o-hidden border-0 shadow-lg my-5"> <div class="card-body p-0"> <div class="row"> <div class="col-lg-12"> <div class="p-5"> <div class="text-center"> <h1 class="h4 text-gray-900 mb-4">Login</h1> </div> <form class="user" action="/login", method="post"> <div class="form-group"> <input type="text" class="form-control form-control-user" id="username" aria-describedby="emailHelp" placeholder="Enter Username..." name="username"> </div> <div class="form-group"> <input type="password" class="form-control form-control-user" id="password" placeholder="Password" name="password"> </div> <div class="form-group"> {{ recaptcha }} </div> <div class="text-center"> {{amends}} </div> <input class="btn btn-primary btn-user btn-block" type="submit" value="Log In"/> </form> <hr> <div class="text-center"> <a class="small" href="/signup">Create an Account!</a> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </body> </html>

Here's my profile.html code:

<html> <head><title>Profile</title> </head> <body id="page-top"> <div id="wrapper"> {% if session["Username"]: %} {% include 'nav.html' %} {% else %} {% include 'notnav.html' %} {% endif %} <div class="container-fluid"> <center> <img class="img-profile rounded-circle" src="{{av|default("/static/images/logo.png")}}" height="90" width="90"> <h1><!--User:--> {% set adminList = ["me", "metoo"] %} {% if username in adminList: %} <span class="badge badge-primary badge-counter"><i class="fas fa-crown"></i></span> {% endif %} <!--{{prefix}}--> {{username}}</h1><br> <a href="/user/{{username}}" class="btn btn-primary btn-icon-split"> <span class="icon text-white-50"> <i class="fas fa-eye"></i> </span> <span class="text">Your Public Profile</span> </a><br> <br> <h3>Email: {{email}}</h3> <br><br> {% if des: %} <center><div class="card" style="background-color:#7289da; color:#2c2f33;"><center><br><h2 class="text-gray-300"> <i class="fab fa-discord fa-x text-gray-300" style="color: #7289da;"></i> Discord</h2><hr><h3>{{tag}}</h3><h3><img class="img-profile rounded-circle" src="{{avatar}}" height="75" width="75"></h3></center></div></center> <br><br> {% endif %} {% if des == False: %} <center><a href="/discord" style="text-decoration:none;"><div class="card" style="padding:10px; margin:10px; background-color:#7289da; color:#2c2f33;"><br><center><h3 class="text-gray-300"> <i class="fab fa-discord fa-x text-gray-300" style="color: #7289da;"></i> Link Your Account With Discord</h3></center><br></div></a></center> <br><br> {% endif %} <br><br> <div class="col-lg-12"> <div class="card shadow mb-4"> <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between"> <h6 class="m-0 font-weight-bold text-primary">Practice Scores</h6> <div class="dropdown no-arrow"> <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <i class="fas fa-ellipsis-v fa-sm fa-fw text-gray-400"></i> </a> <div class="dropdown-menu dropdown-menu-right shadow animated--fade-in" aria-labelledby="dropdownMenuLink"> <div class="dropdown-header">Actions</div> <!--<a class="dropdown-item" href="#">Copy to Clipboard</a>--> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/profile">Refresh</a> </div> </div> </div> <div class="card-body" style="padding:10px; margin:10px; width:85%"> <table class="table table-bordered" id="dataTable" width="95%" cellspacing="0"> <thead> <tr> <th>Name</th> <th>Distribution</th> <th>Score</th> </tr> </thead> <tbody> {% for i in status: %} <tr> <td>{{i[0]}}</td> <td>{{i[1]}}</td> <td>{{i[2]}}</td> </tr> {% endfor %} </tbody> </table> </div> </div> </div> <div class="card shadow mb-4" id="settings" style="padding:10px; margin:10px; width:85%"> <center><h3>Settings</h3></center><br> <form action="/profile", method="post"> Appearance {{ang}} <select id="appearance" name="appearance" class="form-control form-control-user"> {% set opts = ["Sync", "Light", "Dark"] %} <option value="{{ang}}">{{ang}}</option> {% for i in opts: %} {% if i != ang: %} <option value="{{i}}">{{i}}</option> {% endif %} {% endfor %} </select> <br><br> Set your status <textarea id="stat" name="stat" rows="4" cols="50" class="form-control form-control-user"> {{oldstat}} </textarea> <br> Display Discord Information <select id="determine" name="determine" class="form-control form-control-user"> {% if des: %}<option value="yes">Yes</option>{% endif %} <option value="no">No</option> </select> <br><br> <input type="submit" value="Save Changes" class="btn btn-primary btn-user btn-block"/> </form> <br> </div> <br> <div class="row"> <div class="col-xl-12 col-md-12 mb-12"> <a href="" style="text-decoration:none;"><div class="card border-left-primary shadow h-100 py-2"> <div class="card-body"> <div class="row no-gutters align-items-center"> <div class="col mr-2"> <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> AVATAR</div> <div class="h6 mb-0 font-weight-bold text-gray-800">Wanna change your avatar? Head over to Gravatar to set your Gravatar for this site!</div> </div> <div class="col-auto"> <i class="fab fa-grav fa-2x text-gray-300" style="color: #7289da;"></i> </div> </div> </div> </div></a> </div> </div> <br> {% if des == False: %} <div class="row"> <div class="col-xl-12 col-md-12 mb-12"> <a href="/discord" style="text-decoration:none;"><div class="card border-left-primary border-right-primary shadow h-100 py-2" style="background-color:#2c2f33;"> <div class="card-body" style="background-color:#2c2f33;"> <div class="row no-gutters align-items-center"> <div class="col mr-2"> <div class="text-xs font-weight-bold text-primary text-uppercase mb-1"> CONNECT DISCORD</div> <div class="h6 mb-0 font-weight-bold text-gray-100">If you haven't connected Your Discord Account yet, we suggest doing so. Connecting your Discord Account enables us to provide further unrestricted access to certain pages. You can always decide if you want to show your Discord Account on your public profile or not!</div> </div> <div class="col-auto"> <i class="fab fa-discord fa-2x text-gray-300" style="color: #7289da;"></i> </div> </div> </div> </div></a> </div> </div> <br>{% endif %}<br><br><br> </center> </div> </div> </body> </html>

I can't understand why it works for the login page and not for the profile page. I would like to know where I'm going wrong and what I can do to fix this error!

1 year ago




I've manually set my SECRET_KEY so I don't think thats a reason for the data not persisting either...

1 year ago
Load more