I'm aware that you can get session variables using request.session['variable_name']
, but there doesn't seem to be a way to grab the session id(key) as a variable in a similar way. Is this documented anywhere? I can't find it.

- 22,221
- 10
- 124
- 129

- 4,806
- 10
- 33
- 31
9 Answers
request.session.session_key
Note the key will only exist if there is a session, no key, no session. You can use this to test if a session exists. If you want to create a session, call create.
-
3@aehike: `request.session._session_key` as per the answer from @Vinicius – hughes May 02 '13 at 20:18
-
3`request.session.session_key` works fine in Django 1.6. – ecstaticpeon Aug 04 '14 at 14:21
-
9``request.session.session_key`` also works fine in Django 1.7. – Andrew E Dec 02 '14 at 08:10
-
1@p1l0t Umm.. You went too far ahead.. That's something to come in future. – Rohit Jain Jul 01 '15 at 12:38
-
Where is this documented? – ShreevatsaR Aug 20 '15 at 18:13
-
1Document here: https://docs.djangoproject.com/en/dev/topics/http/sessions/#using-sessions-out-of-views – Rockallite Sep 14 '15 at 03:16
-
False. `request.session.session_key` resolves to `None` in Django 1.8. – Dan Loewenherz Sep 24 '15 at 13:32
-
@Dan Using it without problem in 1.8 `request.session.session_key`. – MiniGunnR Nov 09 '15 at 06:25
-
6I prefer using `request.session._get_or_create_session_key()` because, it can happen that there is just no ID yet. – patroqueeet Feb 06 '16 at 07:33
-
2Whether this works or not entirely depends on whether the session has been created. See my answer – Greg Sep 25 '16 at 22:44
-
Why is the session being accessed like an object when `request.session` is a dictionary-like object? – binny May 08 '21 at 03:25
Django sessions save their key in a cookie. At least its middleware extracts it like this:
from django.conf import settings
session_key = request.COOKIES[settings.SESSION_COOKIE_NAME]

- 12,097
- 7
- 42
- 71
in Django >= 1.4 use:
request.session._session_key

- 595
- 5
- 8
-
12Erm, the main intent of private variables (ones starting with an underscore) is that you should **not** use them. – Michał Górny Jul 31 '13 at 09:54
-
1@MichałGórny so what's the alternative? if there is none, I don't see how we have a choice... – Mark May 14 '14 at 09:19
-
1The alternative is to write proper code. If django doesn't provide a *public* API to obtain the session key, it simply means you aren't supposed to use that key. I can't tell you more without knowing what exactly you are trying to do but then it's probably a field for separate question. – Michał Górny May 14 '14 at 14:06
This will either get you a session ID or create one for you. If you do dir(request.session)
, you will get many useful methods.
['TEST_COOKIE_NAME', 'TEST_COOKIE_VALUE', '__class__', '__contains__',
'__delattr__', '__delitem__', '__dict__', '__doc__', '__format__',
'__getattribute__', '__getitem__', '__hash__', '__init__', '__module__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__',
'_get_new_session_key', '_get_or_create_session_key', '_get_session',
'_get_session_key', '_hash', '_session', '_session_key', 'accessed',
'clear', 'create', 'cycle_key', 'decode', 'delete', 'delete_test_cookie',
'encode', 'exists', 'flush', 'get', 'get_expire_at_browser_close',
'get_expiry_age', 'get_expiry_date', 'has_key', 'items', 'iteritems',
'iterkeys', 'itervalues', 'keys', 'load', 'modified', 'pop', 'save',
'session_key', 'set_expiry', 'set_test_cookie', 'setdefault',
'test_cookie_worked', 'update', 'values']
session_id = request.session._get_or_create_session_key()

- 14,225
- 26
- 79
- 98

- 2,517
- 1
- 24
- 37
To reliably get the session key, you need to make sure the session has been created first. The documentation mentions a .create()
session method, which can be used to make sure there's a session key:
def my_view(request):
if not request.session.session_key:
request.session.create()
print(request.session.session_key)

- 9,963
- 5
- 43
- 46
In Django 1.8:
request.session.session_key
and
request.session._session_key
Both work correctly.

- 321
- 4
- 17
You can get the session key with session_key, _session_key
and _get_session_key()
in Django Views as shown below. *I use Django 4.2.1:
# "views.py"
from django.http import HttpResponse
def my_view(request):
print(request.session.session_key) # w85oia6b5yqj5n2t6n7lpwuhw7lt7ti2
print(request.session._session_key) # w85oia6b5yqj5n2t6n7lpwuhw7lt7ti2
print(request.session._get_session_key()) # w85oia6b5yqj5n2t6n7lpwuhw7lt7ti2
return HttpResponse("Test")
And, you can get the session key with request.session.session_key
in Django Templates as shown below:
{# "templates/index.html #}
{{ request.session.session_key }} {# w85oia6b5yqj5n2t6n7lpwuhw7lt7ti2 #}

- 22,221
- 10
- 124
- 129
You can check in your sessions too:
If "var" in request.session:
Var = request.session['var']
Return httpResponse("set")
Else:
Return httpResponse("there isn't")

- 4,743
- 3
- 30
- 42

- 658
- 12
- 27