Django

Code

Show
Ignore:
Timestamp:
06/07/08 15:28:06 (6 months ago)
Author:
jacob
Message:

Fixed #2548: added get/set_expiry methods to session objects. Thanks, Amit Upadhyay and SmileyChris?.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/sessions.txt

    r7329 r7586  
    8181 
    8282    * ``__getitem__(key)`` 
     83 
    8384      Example: ``fav_color = request.session['fav_color']`` 
    8485 
    8586    * ``__setitem__(key, value)`` 
     87 
    8688      Example: ``request.session['fav_color'] = 'blue'`` 
    8789 
    8890    * ``__delitem__(key)`` 
     91 
    8992      Example: ``del request.session['fav_color']``. This raises ``KeyError`` 
    9093      if the given ``key`` isn't already in the session. 
    9194 
    9295    * ``__contains__(key)`` 
     96 
    9397      Example: ``'fav_color' in request.session`` 
    9498 
    9599    * ``get(key, default=None)`` 
     100 
    96101      Example: ``fav_color = request.session.get('fav_color', 'red')`` 
    97102 
     
    102107    * ``setdefault()`` (**New in Django development version**) 
    103108 
    104 It also has these three methods: 
     109It also has these methods: 
    105110 
    106111    * ``set_test_cookie()`` 
     112 
    107113      Sets a test cookie to determine whether the user's browser supports 
    108114      cookies. Due to the way cookies work, you won't be able to test this 
     
    111117 
    112118    * ``test_cookie_worked()`` 
     119 
    113120      Returns either ``True`` or ``False``, depending on whether the user's 
    114121      browser accepted the test cookie. Due to the way cookies work, you'll 
     
    117124 
    118125    * ``delete_test_cookie()`` 
     126 
    119127      Deletes the test cookie. Use this to clean up after yourself. 
     128 
     129    * ``set_expiry(value)`` 
     130 
     131      **New in Django development version** 
     132 
     133      Sets the expiration time for the session. You can pass a number of 
     134      different values: 
     135 
     136            * If ``value`` is an integer, the session will expire after that 
     137              many seconds of inactivity. For example, calling 
     138              ``request.session.set_expiry(300)`` would make the session expire 
     139              in 5 minutes. 
     140 
     141            * If ``value`` is a ``datetime`` or ``timedelta`` object, the 
     142              session will expire at that specific time. 
     143       
     144            * If ``value`` is ``0`` then the user's session cookie will expire 
     145              when their browser is closed. 
     146 
     147            * If ``value`` is ``None``, the session reverts to using the global 
     148              session expiry policy. 
     149 
     150    * ``get_expiry_age()`` 
     151 
     152      **New in Django development version** 
     153 
     154      Returns the number of seconds until this session expires. For sessions 
     155      with no custom expiration (or those set to expire at browser close), this 
     156      will equal ``settings.SESSION_COOKIE_AGE``. 
     157 
     158    * ``get_expiry_date()`` 
     159 
     160      **New in Django development version** 
     161 
     162      Returns the date this session will expire. For sessions with no custom 
     163      expiration (or those set to expire at browser close), this will equal the 
     164      date ``settings.SESSION_COOKIE_AGE`` seconds from now. 
     165 
     166    * ``get_expire_at_browser_close()`` 
     167 
     168      **New in Django development version** 
     169 
     170      Returns either ``True`` or ``False``, depending on whether the user's 
     171      session cookie will expire when their browser is closed. 
    120172 
    121173You can edit ``request.session`` at any point in your view. You can edit it 
     
    279331a browser. 
    280332 
     333**New in Django development version** 
     334 
     335This setting is a global default and can be overwritten at a per-session level 
     336by explicitly calling ``request.session.set_expiry()`` as described above in 
     337`using sessions in views`_. 
     338 
    281339Clearing the session table 
    282340==========================