Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: accessing session variables from within a template?

Tags:

django

People also ask

How do you pass variables from Django view to a template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

What is {% include %} in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

How does Django keep track of a session?

Django uses a cookie containing a special session id to identify each browser and its associated session with the site. The actual session data is stored in the site database by default (this is more secure than storing the data in a cookie, where they are more vulnerable to malicious users).


You need to add django.template.context_processors.request to your template context processors. Then you can access them like this:

{{ request.session.name }}

In case you are using custom views make sure you are passing a RequestContext instance. Example taken from documentation:

from django.shortcuts import render_to_response
from django.template import RequestContext

def some_view(request):
    # ...
    return render_to_response('my_template.html',
                              my_data_dictionary,
                              context_instance=RequestContext(request))

Update 2013: Judging by the upvotes I'm still receiving for this answer, people are still finding it helpful, more than three years after it was originally written. Please note however, that although the view code above is still valid, nowadays there is a much simpler way of doing this. render() is a function very similar to render_to_response(), but it uses RequestContext automatically, without a need to pass it explicitly:

from django.shortcuts import render

def some_view(request):
    # ...
    return render(request, 'my_template.html', my_data_dictionary)

request.session is a dictionary like any other, so you just use the normal template mechanism for attributes and members:

{{ request.session.name }}

Don't forget to pass the request into the template context, or even better ensure you are using RequestContext and have the request context processor enabled. See the documentation.


I am using Django 1.9 (March 2016) and to get {{ request.session.name}} to work, my settings have this::

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

The difference from the previous answers is: 'django.core.context_processors.request' became 'django.template.context_processors.request'


You can pass a request variable to a template and there use:

{{ request.session.name }}

the simplest implementation is using if loop :

{% if 'data' in request.session %}

First print request.session.keys() then

request.session['_auth_user_id']
request.session['_auth_user_backend']

You will get these two session variables.


In your settins.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)

Your view, maybe look like this.

from django.shortcuts import render_to_response, render
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext

@login_required()
def index_admin(request):
    return render_to_response('carteras/index_admin.html', {}, context_instance=RequestContext(request))