英文:
Why is Editing SessionStore Between Views in Django not Working
问题
我在django中有一个会话,使用django.sessions
,我希望使用SessionStore
进行修改,但是当我修改存储并刷新页面时(页面自然由django的会话中间件更新),值没有持久化。
例如,这是我的初始响应,我设置了cookie,这样我就可以在下一个视图中获取SessionStore
(似乎在这样一起使用Cookie和Session可能有些奇怪,但我有我的原因与此帖无关,我无法从最初收集Cookie的视图中访问django.sessions):
next = f"{request.path}"
response = redirect(f"{reverse('quickbooks_authentication', args=[request.client_url])}?quickbooks_account_id={quickbooks_account.pk}&next={next}")
response.set_cookie('session_key', request.session.session_key)
response.set_cookie('client_url', client_url)
return response
然后在下一个视图中,一切都如预期般打印出:
print(request.COOKIES.get('session_key')) # tmcdnowypuzcrndtw117hnjxgsruzddj --有效
store = SessionStore(session_key=request.COOKIES.get('session_key'))
store['testing'] = 'test'
store.save()
print(store.modified) # True
print(store['testing']) # test
return redirect(request.GET.get('next'))
但是当我回到页面时,会话未更新,因为store['testing']
不存在。为什么,我做错了什么?
store = SessionStore(session_key=request.COOKIES.get('session_key')) # 与之前的密钥相同
store['testing'] # 键错误
英文:
I have a session in django using django.sessions
that I wish to alter using the SessionStore
, but when I alter the Store and refresh the page (which is naturally updated by django's session middleware), the values are not persisted.
For example, here is my initial response, I set the cookies so I can gather the SessionStore
in the next view (it may seem off to use Cookies and Sessions together like this, but I have my reasons not related to this post, I am not able to access django.sessions from the view I initially gather the cookies from):
next = f"{request.path}"
response = redirect(f"{reverse('quickbooks_authentication', args=[request.client_url])}?quickbooks_account_id={quickbooks_account.pk}&next={next}")
response.set_cookie('session_key', request.session.session_key)
response.set_cookie('client_url', client_url)
return response
Then in the next view, all is printing out as expected:
print(request.COOKIES.get('session_key')) # tmcdnowypuzcrndtw117hnjxgsruzddj --valid
store = SessionStore(session_key=request.COOKIES.get('session_key'))
store['testing'] = 'test'
store.save()
print(store.modified) # True
print(store['testing']) # test
return redirect(request.GET.get('next'))
But when I go back to the page, the session was not updated as store['testing']
does not exist. Why, what am I doing wrong?
store = SessionStore(session_key=request.COOKIES.get('session_key')) # same key as before
store['testing'] # key error
答案1
得分: 0
只有代码部分需要翻译,以下是代码的翻译:
...
store['testing'] = 'test'
store.save()
# tah dah, now we are good on reload
request.session = store
英文:
It seems silly to me as there is a new query for the SessionStore
in both views using SessionStore(session_key=request.COOKIES.get('session_key'))
, but the solution was to set the actual request.session
to the newly defined/updated Store, like so:
...
store['testing'] = 'test'
store.save()
# tah dah, now we are good on reload
request.session = store
With that change, I am able to view the session changes, along with the default values I already had between the SessionStore(session_key=request.COOKIES.get('session_key'))
object and now request.session
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论