I have a web site which shows different content based on a location the visitor chooses. e.g: User enters in 55812 as the zip. I know what city and area lat/long. that is and give them their content pertinent to that area. My question is how can I store this in a cookie so that when they return they are not required to always enter their zip code?
我有一个网站,根据访问者选择的位置显示不同的内容。e。用户输入55812作为zip文件。我知道那个城市和地区。也就是给他们与该领域相关的内容。我的问题是,我如何将它存储在一个cookie中,以便当它们返回时,它们不需要总是输入它们的邮政编码?
I see it as follows:
我的看法如下:
- Set persistent cookie based on their area.
- 根据它们的区域设置持久cookie。
- When they return read cookie, grab zipcode.
- 当他们返回读取cookie时,获取zipcode。
- Return content based on the zip code in their cookie.
- 根据其cookie中的邮政编码返回内容。
I can't seem to find any solid information on setting a cookie. Any help is greatly appreciated.
我似乎找不到任何关于设置cookie的可靠信息。非常感谢您的帮助。
3 个解决方案
#1
48
This is a helper to set a persistent cookie:
这是设置持久cookie的助手:
import datetime
def set_cookie(response, key, value, days_expire = 7):
if days_expire is None:
max_age = 365 * 24 * 60 * 60 #one year
else:
max_age = days_expire * 24 * 60 * 60
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie(key, value, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None)
Use the following code before sending a response.
在发送响应之前,请使用以下代码。
def view(request):
response = HttpResponse("hello")
set_cookie(response, 'name', 'jujule')
return response
UPDATE : check @Peter answer below for a builtin solution : https://*.com/a/5575578/174027
更新:查看下面的@Peter的答案,以获得构建解决方案:https://*.com/a/5575578/174027
#2
207
Using Django's session framework should cover most scenarios, but Django also now provide direct cookie manipulation methods on the request and response objects (so you don't need a helper function).
使用Django的会话框架应该涵盖大部分场景,但是Django现在还为请求和响应对象提供了直接的cookie操作方法(因此不需要助手函数)。
Setting a cookie:
设置一个cookie:
def view(request):
response = HttpResponse('blah')
response.set_cookie('cookie_name', 'cookie_value')
Retrieving a cookie:
检索一个cookie:
def view(request):
if 'cookie_name' in request.COOKIES:
value = request.COOKIES['cookie_name']
#3
18
You could manually set the cookie, but depending on your use case (and if you might want to add more types of persistent/session data in future) it might make more sense to use Django's sessions feature. This will let you get and set variables tied internally to the user's session cookie. Cool thing about this is that if you want to store a lot of data tied to a user's session, storing it all in cookies will add a lot of weight to HTTP requests and responses. With sessions the session cookie is all that is sent back and forth (though there is the overhead on Django's end of storing the session data to keep in mind).
您可以手动设置cookie,但是依赖于您的用例(如果您可能想在将来添加更多类型的持久性/会话数据),使用Django的会话特性可能更有意义。这将使您获得并设置与用户会话cookie内部绑定的变量。很酷的一点是,如果您想要存储与用户会话相关的大量数据,那么将所有数据存储在cookie中将会给HTTP请求和响应增加很多重量。对于会话,会话cookie就是来回发送的所有内容(尽管Django在存储会话数据方面存在开销)。
#1
48
This is a helper to set a persistent cookie:
这是设置持久cookie的助手:
import datetime
def set_cookie(response, key, value, days_expire = 7):
if days_expire is None:
max_age = 365 * 24 * 60 * 60 #one year
else:
max_age = days_expire * 24 * 60 * 60
expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=max_age), "%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie(key, value, max_age=max_age, expires=expires, domain=settings.SESSION_COOKIE_DOMAIN, secure=settings.SESSION_COOKIE_SECURE or None)
Use the following code before sending a response.
在发送响应之前,请使用以下代码。
def view(request):
response = HttpResponse("hello")
set_cookie(response, 'name', 'jujule')
return response
UPDATE : check @Peter answer below for a builtin solution : https://*.com/a/5575578/174027
更新:查看下面的@Peter的答案,以获得构建解决方案:https://*.com/a/5575578/174027
#2
207
Using Django's session framework should cover most scenarios, but Django also now provide direct cookie manipulation methods on the request and response objects (so you don't need a helper function).
使用Django的会话框架应该涵盖大部分场景,但是Django现在还为请求和响应对象提供了直接的cookie操作方法(因此不需要助手函数)。
Setting a cookie:
设置一个cookie:
def view(request):
response = HttpResponse('blah')
response.set_cookie('cookie_name', 'cookie_value')
Retrieving a cookie:
检索一个cookie:
def view(request):
if 'cookie_name' in request.COOKIES:
value = request.COOKIES['cookie_name']
#3
18
You could manually set the cookie, but depending on your use case (and if you might want to add more types of persistent/session data in future) it might make more sense to use Django's sessions feature. This will let you get and set variables tied internally to the user's session cookie. Cool thing about this is that if you want to store a lot of data tied to a user's session, storing it all in cookies will add a lot of weight to HTTP requests and responses. With sessions the session cookie is all that is sent back and forth (though there is the overhead on Django's end of storing the session data to keep in mind).
您可以手动设置cookie,但是依赖于您的用例(如果您可能想在将来添加更多类型的持久性/会话数据),使用Django的会话特性可能更有意义。这将使您获得并设置与用户会话cookie内部绑定的变量。很酷的一点是,如果您想要存储与用户会话相关的大量数据,那么将所有数据存储在cookie中将会给HTTP请求和响应增加很多重量。对于会话,会话cookie就是来回发送的所有内容(尽管Django在存储会话数据方面存在开销)。