-2

I want to show different header for person with logged in and person who logged out in Flask. Could I use the session variable directly in the jija2 template.

I have used different name for session since , another session variable name used for sqlalchamy session.

from flask import session as usersession 

I have tried accessing the usersession variable but it's saying undefined. but when I use the session['username'] I could access the session variable.

further when I pop from usersession It's not popping from the session. Still session accessed in the tamplete has the username variable

usersession.pop('email', None) usersession.pop('type', None) jinja2.exceptions.UndefinedError: 'usersession' is undefined 

My code is below .

{% if usersession['username'] is not none %} {% include "store/headers/loginheader.html" %} {% else %} {% include "store/headers/logoutheader.html" %} {% endif %} 
0

1 Answer 1

0

In your views function, try usersession[‘username’] = ‘username

And in the template:

{{ usersession[‘username’] }} 

For sessions make sure you have set up the environment correctly to be able to access sessions (ie a secret key).

Also have a look at flask-login as their ‘current_user’ object has great functionality and may be what you’re looking for

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.