搜尋此網誌

2015年11月15日 星期日

Django Session 沒有儲存變更的資料

你是不是也有發生過在 session 修改 dict 資料,送出後
session 的資料卻不見了!這是因為 Django的 session 預設只會修改被賦值或是被刪除的資料
像是以下的情況

# session 有修改
request.session['hello'] = 'world'

# session 有修改
del request.session['hello]

# session 有修改
request.session['hello'] = {}

# session 沒有修改!!!!
# 因為是request.session['hello']被賦予新的值,而不是request.session
request.session['hello']['world'] = 'hello'

在這個情況下,如果我們要讓session能夠被修改,我們需要設定session的modified的屬性。
request.session.modified = True

如果要修改這個預設的方式,讓session 能夠每次都被儲存,那我們需要設定 SESSION_SAVE_EVERY_REQUEST 為 True.

1 則留言:

  1. The three figured that they could do extra than simply ship a one-time supply of printers to Lviv. Kranz, who organized the initiative and is main the fundraising, says they’ve raised greater than $50,000 with hopes of bringing in more than $350,000 from individual donors and organizations. Burgs also has entry to 70 of best fins for free diving his company’s 3D printers at facilities in Poland. If you are looking to|want to|wish to} buy a 3D printer but are not positive which is best for you, take a look at|try} our purchasing guide to get answers to all of your questions! We cover the variations in filament materials and printing applied sciences intimately so you'll be able to|you probably can} choose the 3D printer that provides every thing you need.

    回覆刪除

對於文章內有任何問題,都可以提出來討論看看哦。