176👍
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.
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]
- [Django]-Django select_for_update cannot be used outside of a transaction
- [Django]-Django: Using F arguments in datetime.timedelta inside a query
- [Django]-Add Text on Image using PIL
- [Django]-Creating a JSON response using Django and Python
- [Django]-What is the purpose of apps.py in Django 1.9?
- [Django]-How do I run tests against a Django data migration?
13👍
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()
- [Django]-Change a Django form field to a hidden field
- [Django]-How to pull a random record using Django's ORM?
- [Django]-Python Socket.IO client for sending broadcast messages to TornadIO2 server
13👍
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)
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
- [Django]-Using django-rest-interface
- [Django]-How to check if ManyToMany field is not empty?
- [Django]-Add inline model to django admin site
- [Django]-<Django object > is not JSON serializable
3👍
In Django 1.8:
request.session.session_key
and
request.session._session_key
Both work correctly.
- [Django]-Django models: mutual references between two classes and impossibility to use forward declaration in python
- [Django]-__init__() got an unexpected keyword argument 'mimetype'
- [Django]-Django-taggit – how do I display the tags related to each record
0👍
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 #}
- [Django]-How to get the current URL within a Django template?
- [Django]-Include intermediary (through model) in responses in Django Rest Framework
- [Django]-Django-celery: No result backend configured
-1👍
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")
- [Django]-Django unit tests without a db
- [Django]-Are there any plans to officially support Django with IIS?
- [Django]-Does SQLAlchemy have an equivalent of Django's get_or_create?