添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
私奔的柳树  ·  python 程序 等待 ...·  2 小时前    · 
风度翩翩的凳子  ·  python ...·  2 小时前    · 
爱喝酒的紫菜汤  ·  area_2·  2 周前    · 
玩滑板的荔枝  ·  niubility-coding-js/Ja ...·  1 月前    · 
怕老婆的四季豆  ·  【转】C# RSA ...·  1 月前    · 
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") If you will eventually deploy your app in a multi-process setup, your random initialization of the secret key won't work. Klaus D. Jan 31, 2021 at 8:40

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 .