easy set cookie expired time

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django.conf import settings
import datetime

def set_cookie(response, key, value, expire=None):
    if expire is None:
        max_age = 365*24*60*60  #one year
    else:
        max_age = expire
    expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
    response.set_cookie(key, value, max_age=max_age, expires=expires, 
        domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None)
    

More like this

  1. Extend generic view object_list to support paginate_by via cookies by kersurk 1 year ago
  2. Captcha Middleware (Template) by zeeg 4 years, 10 months ago
  3. ActiveManager: filter objects depending on publication and/or expiration dates by haplo 3 years, 7 months ago
  4. Captcha Middleware by zeeg 4 years, 10 months ago
  5. Amazon's CloudFront streaming signed urls by sayane 1 year, 5 months ago

Comments

xtealc (on November 10, 2008):

thank u so much :)

#

(Forgotten your password?)