In Django, how can I find out the request.session sessionid and use it as a variable?

前端 未结 8 865
借酒劲吻你
借酒劲吻你 2020-12-13 03:08

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 sessionid as a variable

8条回答
  •  自闭症患者
    2020-12-13 03:47

    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()
    

提交回复
热议问题