-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 %}
Sathiyakugan
  • 674
  • 7
  • 20

1 Answers1

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

koopmac
  • 936
  • 10
  • 27