3

作業中のサイトがあり、Cookie に値を保存したい

これは数値です。ユーザーが Web サイトにアクセスしたときに、最後の訪問時の数値を知りたいので、ユーザーがサイトにアクセスしたときに現在の値を保存する永続的な Cookie を用意することを考えています。セッション Cookie がない場合、セッション Cookie は永続 Cookie のコピーを取得します。このようにして、セッション Cookie は常に最後の訪問からの値を持ちます。

有効期限を 1 年後に設定したにもかかわらず、永続的な Cookie が永続化されていないようです。

ここに私のpythonコードがあります:

persistentCookieKey = category + '_highest_id'
sessionCookieKey = 'session_' + persistentCookieKey + '_highest_id'

persistentCookieValue = request.get_cookie(persistentCookieKey)
if persistentCookieValue == None:
    persistentCookieValue = 0      # each time i restart my browser it comes through here!

sessionCookieValue = request.get_cookie(sessionCookieKey)
print 'persistentCookieValue:', persistentCookieValue
print 'sessionCookieValue:', sessionCookieValue

if sessionCookieValue == None:
    print 'session cookie not set, setting to:', persistentCookieValue
    sessionCookieValue = persistentCookieValue
    response.set_cookie(sessionCookieKey, str(persistentCookieValue))

print 'setting persistent cookie to value:', highestId
expireDate = date.today() + timedelta(days=365)
response.set_cookie(persistentCookieKey, str(highestId), expires=expireDate)

highestIdLastVisit = int(sessionCookieValue) 
4

1 に答える 1

9

ボトルはhttp://docs.python.org/library/cookie.htmlを使用して Cookie サポートを実装します。この実装では、expiresパラメーターがWdy, DD-Mon-YY HH:MM:SS GMT形式の文字列である必要があります。datetime または date オブジェクトを渡すと、サイレントに失敗します。

私はボトルの将来のバージョンでそれを修正します(こんにちは、私は作者です)が、今のところmax_age代わりに使用することをお勧めします.

編集:ああ、それが間違って文書化されていることに気づきました。そのために残念。Edit2: 修正済み (マスターで)

于 2011-10-27T09:52:19.020 に答える