Using flask_login session with jinja2 templates

15,880

Flask-Login adds the current_user variable to your templates:

{% if current_user.is_authenticated %}
    ...
{% else %}
    ...
{% endif %}

They mention this briefly in the documentation.

Share:
15,880

Related videos on Youtube

Slow Harry
Author by

Slow Harry

Updated on June 14, 2022

Comments

  • Slow Harry
    Slow Harry almost 2 years

    I have simple jinja2 template with registration/login links, I should hide them when user logged in, I also use flask_login module for this stuff.

    Question is: How should I identify is user logged in in jinja2 templates?

  • Devi
    Devi almost 9 years
    Flask-login docs do specify that You can then access the logged-in user with the current_user proxy, which is available in every template
  • anvd
    anvd over 8 years
    Now is {% if current_user.is_authenticated %}
  • deesolie
    deesolie over 3 years
    Note that when using {{ current_user.is_authenticated }} in javascript, that the value returned is not understood and must be put in a a string such as "{{ current_user.is_authenticated }}". Came across this when validating onclick ajax requests for logged in users.