[英]Using Django session key to use request sessions between views
Is it a good idea to store django _session_key
in another model as an identifier for specific session. 将django
_session_key
存储在另一个模型中作为特定会话的标识符是一个好主意。
I am using django _session_key
to store a unique session inside a view and then I am saving the _session_key
in another object . 我正在使用django
_session_key
在视图中存储唯一的会话,然后将_session_key
保存在另一个对象中。
def myview(request):
if request.method == "POST":
myform = Myform(request.form)
if myform.is_valid():
name = myform.cleaned_data['name']
title = myform.cleaned_data['title']
author_session = request.session._session_key
# Creating a model object
model1(name=name, title=title, author_session=author_session).save()
return HttpResponseRedirect(reverse('myview2',
kwargs={'name':model1.name}))
else:
# Some renders
else:
# Some other renders
def myview2(request, name):
obj1 = model1.objects.get(name=name)
if request.session._session_key == obj1.author_session:
# Some render
else:
# Some other render
Now ,I am thinking is this a good idea to use _session_key
as unique identity for sessions between different views . 现在,我想将
_session_key
用作不同视图之间的会话的唯一标识是一个好主意。 Is there any other way to identify unique session between views ? 还有其他方法可以识别视图之间的唯一会话吗?
PS- I have read that using _session_key
is generally disregarded. PS-我读到使用
_session_key
通常被忽略。
Also please suggest how to write tests for the sessions between views 还请建议如何为视图之间的会话编写测试
No, this is entirely backwards. 不,这完全是倒退。 You should store the key of the model1 instance in the session in the first view, and get it out in the second.
您应该在第一个视图中将model1实例的密钥存储在会话中,然后在第二个视图中将其取出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.