Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access session variables in jinja 2 - Flask

Tags:

Do I need to pass session variables manually from Flask to my HTML or are they automatically sent in some way?

Can I do

return render_template('index.html') 

and access a session variable username, for example, like {{session['username'}} in the HTML file?

like image 495
bpb101 Avatar asked Feb 02 '17 21:02

bpb101


People also ask

How do I get data from a session in Flask?

To release a session variable use pop() method. The following code is a simple demonstration of session works in Flask. URL '/' simply prompts user to log in, as session variable 'username' is not set. As user browses to '/login' the login() view function, because it is called through GET method, opens up a login form.

How do I find my session id in Flask?

There is no session id. Sessions in Flask are simply wrappers over cookies. What you save on it it's digitally signed and sent as a cookie to the client. When you make a request, that cookie is sent to your server and then verified and transformed in a Python object.


1 Answers

In python

session['username'] = 'username' 

in jinja2 you can go

{{session['username']}} 
like image 89
bpb101 Avatar answered Sep 18 '22 12:09

bpb101