diango的session默认存储在内置django_session
表中,可以设置存储在redis中👇。
0,在环境中安装一个包
pip install django-redis-sessions==0.5.6
1,在settings中配置
SESSION_ENGINE = "redis_sessions.session"
SESSION_REDIS_HOST = "localhost"
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 15
SESSION_REDIS_PASSWORD = ""
SESSION_REDIS_PREFIX = "session"
2,设置session
request.session['username'] = "li"
request.session['age'] = 20
3,取session
username = request.session.get('username')
age= request.session.get('age')
print(user,age)
# li 20
4,验证
import redis
pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=15)
redis = redis.Redis(connection_pool=pool)
print(redis.keys())
#[b'session:7fgwiet5yj3ghpxuoazu66g93iwrtn8v']
print(redis.get("session:7fgwiet5yj3ghpxuoazu66g93iwrtn8v"))
#b'OGRjZDhkNjJmMmJhYjAzMTZmODYwNmIzYTU5ODBkNDA3MWVmYmE0Nzp7InVzZXJuYW1lIjoibGkiLCJhZ2UiOjE4fQ=='
网友评论