I'd like to set a cookie via Django with that has several different values to it, similar to .NET's HttpCookie.Values property. Looking at the documentation, I can't tell if this is possible. It looks like it just takes a string, so is there another way?
我想通过Django设置一个cookie,它有几个不同的值,类似于.NET的HttpCookie.Values属性。看文档,我不知道这是否可行。看起来它只需要一个字符串,那么还有另外一种方法吗?
I've tried passing it an array ([10, 20, 30]
) and dictionary ({'name': 'Scott', 'id': 1}
) but they just get converted to their string format. My current solution is to just use an arbitrary separator and then parse it when reading it in, which feels icky. If multi-values aren't possible, is there a better way? I'd rather not use lots of cookies, because that would get annoying.
我试过传递一个数组([10,20,30])和字典({'name':'Scott','id':1}),但它们只是转换为字符串格式。我目前的解决方案是只使用一个任意的分隔符,然后在读取它时解析它,这感觉很棘手。如果无法使用多值,是否有更好的方法?我宁愿不使用大量的cookie,因为那会很烦人。
4 个解决方案
#1
6
.NETs multi-value cookies work exactly the same way as what you're doing in django using a separator. They've just abstracted that away for you. What you're doing is fine and proper, and I don't think Django has anything specific to 'solve' this problem.
.NETs多值cookie的工作方式与使用分隔符在django中执行的方式完全相同。他们只是为你抽象出来。你正在做的事情是正确的,我不认为Django有任何具体的'解决'这个问题。
I will say that you're doing the right thing, in not using multiple cookies. Keep the over-the-wire overhead down by doing what you're doing.
我会说你做的是正确的,不使用多个cookie。做你正在做的事情,保持线上的开销。
#2
1
If you're looking for something a little more abstracted, try using sessions. I believe the way they work is by storing an id in the cookie that matches a database record. You can store whatever you want in it. It's not exactly the same as what you're looking for, but it could work if you don't mind a small amount of db overhead.
如果您正在寻找更抽象的东西,请尝试使用会话。我相信他们的工作方式是在cookie中存储与数据库记录匹配的id。你可以存储你想要的任何东西。它与您正在寻找的并不完全相同,但如果您不介意少量的数据包开销,它可能会有效。
#3
1
(Late answer!)
This will be bulkier, but you call always use python's built in serializing.
这将更笨重,但你总是使用python的内置序列化。
You could always do something like:
你总是可以这样做:
import pickle
class MultiCookie():
def __init__(self,cookie=None,values=None):
if cookie != None:
try:
self.values = pickle.loads(cookie)
except:
# assume that it used to just hold a string value
self.values = cookie
elif values != None:
self.values = values
else:
self.values = None
def __str__(self):
return pickle.dumps(self.values)
Then, you can get the cookie:
然后,您可以获取cookie:
newcookie = MultiCookie(cookie=request.COOKIES.get('multi'))
values_for_cookie = newcookie.values
Or set the values:
或者设置值:
mylist = [ 1, 2, 3 ]
newcookie = MultiCookie(values=mylist)
request.set_cookie('multi',value=newcookie)
#4
0
Django does not support it. The best way would be to separate the values with arbitrary separator and then just split the string, like you already said.
Django不支持它。最好的方法是用任意分隔符分隔值,然后就像你已经说过的那样分割字符串。
#1
6
.NETs multi-value cookies work exactly the same way as what you're doing in django using a separator. They've just abstracted that away for you. What you're doing is fine and proper, and I don't think Django has anything specific to 'solve' this problem.
.NETs多值cookie的工作方式与使用分隔符在django中执行的方式完全相同。他们只是为你抽象出来。你正在做的事情是正确的,我不认为Django有任何具体的'解决'这个问题。
I will say that you're doing the right thing, in not using multiple cookies. Keep the over-the-wire overhead down by doing what you're doing.
我会说你做的是正确的,不使用多个cookie。做你正在做的事情,保持线上的开销。
#2
1
If you're looking for something a little more abstracted, try using sessions. I believe the way they work is by storing an id in the cookie that matches a database record. You can store whatever you want in it. It's not exactly the same as what you're looking for, but it could work if you don't mind a small amount of db overhead.
如果您正在寻找更抽象的东西,请尝试使用会话。我相信他们的工作方式是在cookie中存储与数据库记录匹配的id。你可以存储你想要的任何东西。它与您正在寻找的并不完全相同,但如果您不介意少量的数据包开销,它可能会有效。
#3
1
(Late answer!)
This will be bulkier, but you call always use python's built in serializing.
这将更笨重,但你总是使用python的内置序列化。
You could always do something like:
你总是可以这样做:
import pickle
class MultiCookie():
def __init__(self,cookie=None,values=None):
if cookie != None:
try:
self.values = pickle.loads(cookie)
except:
# assume that it used to just hold a string value
self.values = cookie
elif values != None:
self.values = values
else:
self.values = None
def __str__(self):
return pickle.dumps(self.values)
Then, you can get the cookie:
然后,您可以获取cookie:
newcookie = MultiCookie(cookie=request.COOKIES.get('multi'))
values_for_cookie = newcookie.values
Or set the values:
或者设置值:
mylist = [ 1, 2, 3 ]
newcookie = MultiCookie(values=mylist)
request.set_cookie('multi',value=newcookie)
#4
0
Django does not support it. The best way would be to separate the values with arbitrary separator and then just split the string, like you already said.
Django不支持它。最好的方法是用任意分隔符分隔值,然后就像你已经说过的那样分割字符串。