How to set a cookie and then redirect using Python?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kim Rowden
    New Member
    • Jan 2011
    • 2

    How to set a cookie and then redirect using Python?

    I've read that Python does not alllow one to set a cookie and then redirect. I'm trying to save the current page location before pushing the user to the signin page.

    I'm coming from ASP, which allows for this in a very simple manner:
    Code:
    if request.cookies("GDtoken") = '' then
      response.cookies("cpage") = request.servervariables("PATH_INFO")
      response.redirect "signin.asp"
    end if
    ... I'm surprised that I couldn't find a workaround or a solution for Python.

    Here's my Python code:
    Code:
    if 'GDtoken' not in cookie:
      #save the location of this page to the cookie
      cookie['cpage'] = os.environ.get('SCRIPT_NAME', '/')
      cookie['cpage']['expires'] = 60*30
      cookie['cpage']['path'] = '/'
    	
      #save the continuation page cookie
      print 'Status: 302 Moved Temporarily'
      print cookie.output()
    
      #and redirect user to the signin page
      print 'Location: signin.py'
      print
      exit()
    The cookie is not set when I get to signin.py
    How would one do this in Python?

    Thanks!
Working...