How to Get Django Session Data in Template

Often you will need to access Django session data inside your templates. To do this access request.session followed by the name of the property to get.

 

Display Session String Data in a Django Template

Let's say you have a string stored in a session property. You could access it in your templates like this:

 

{{ request.session.prop_name }}

 

Session If Statement in Template

Let's say you have a boolean session property and would like to evaluate it in an if statement in your templates. You could use it like this:

 

{% if request.session.dark_mode == True %}
  <!-- Do something here... -->
{% endif %}
django