Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

443
Visualizações
How to access session variables in jinja 2 - Flask

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?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

In python

session['username'] = 'username'

in jinja2 you can go

{{session['username']}}
over 4 years ago · Santiago Trujillo Relatório

0

If you want to isolate certain parts of your HTML using session you can call the session directly on those elements:

{% if session['username'] %}
<li>Logout</li>
{% endif %}
over 4 years ago · Santiago Trujillo Relatório

0

@bpb101 is correct on the Jinja2 format (though left out the spaces as others have mentioned). In the HTML/Jinja2 template you can simply call the session dictionary without passing it to the template:

{{ session['username'] }}

However the other example, in the Python code, would actually overwrite the value of session['username'] with the string 'username', due to variable assignment. If you were trying to set a variable to the value of session['username'] you would use:

username = session['username']

Otherwise if you just needed to test or use the value for some other reason you can access it directly for example:

if session['username'] == some_value:
    return "The username is", session['username']

Hopefully that helps for anyone new to Flask or Jinja2 that might wonder why their session variables are being overwritten.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda