Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Im attempting to create a website in flask. To do that, I need use sessions and therefore I am required to use a secret key. I did all of that, and yet it still returns a long error log with
nameerror: name ‘session’ is not defined
at then end
I tried everything and moved the thing that sets the secret key everywhere, but it always had the same issue. Here is my code currently:
from flask import Flask
import os
app = Flask(__name__)
app.secret_key = os.urandom(24)
usernumber = 0
@app.route('/')
def homepage():
global usernumber
session['usernumber'] = usernumber
usernumber = usernumber + 1
Usernumberstring = session['usernumber']
return f"Welcome {Usernumberstring}"
if __name__ == '__main__':
app.run(use_reloader=True, debug=False, host="0.0.0.0")
–
You are not importing session;
from flask import Flask, session
Check more details about flask session here;
https://pythonbasics.org/flask-sessions/#:~:text=Unlike%20cookies%2C%20Session%20(session),temporary%20directory%20on%20the%20server
.
Thanks for contributing an answer to Stack Overflow!
-
Please be sure to
answer the question
. Provide details and share your research!
But
avoid
…
-
Asking for help, clarification, or responding to other answers.
-
Making statements based on opinion; back them up with references or personal experience.
To learn more, see our
tips on writing great answers
.