Django基础笔记5(Session)
Session
cookie:保存在客户端浏览器上的键值对
session:保存在服务器端的数据
保持会话
def index(req):
v = req.session.get('username')
if v:
return render(req, './App4/index.html', {'user': v})
else:
return render(req, './App4/login.html', {'msg': ''})
def login(request):
if request.method == 'GET':
return render(request, './App4/login.html', {'msg': ''})
else:
u = request.POST.get('username') # 生成随机串{'username':'lhh'}
p = request.POST.get('password') # 通过session并保存
key = {'username': u, 'password': p}
obj = models.User.objects.filter(**key).first()
if obj:
request.session['username'] = u
request.session['password'] = p
return redirect('./index/')
else:
return render(request, './App4/login.html', {'msg': '用户名或密码错误'})
#设置
def login(request):
if request.method == 'GET':
return render(request, 'App5/login.html')
else:
user = request.POST.get('uname')
pwd = request.POST.get('pwd')
gender = request.POST.get('gender')
week = request.POST.get('week')
if gender == '1':
obj = models.Boy.objects.filter(**{'name': user, 'passwd': pwd}).first()
else:
obj = models.Girl.objects.filter(**{'name': user, 'passwd': pwd}).first()
if obj:
user_info = {'user_id': obj.id, 'gender': gender, 'uname': user, 'nickname': obj.nickname}
request.session['user_info'] = user_info
return redirect('App5/index.html')
#使用
def index(request):
v = request.session.get('user_info')
if not v:
return redirect('./App5/login.html')
else:
gender = v.get('gender')
name = v.get('nickname')
if gender == '1':
user_list = models.Girl.objects.all()
else:
user_list = models.Boy.objects.all()
return render(request, './App5/index.html', {'userList': user_list, 'name': name})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<!--render将request也会传过来,通过request也可以调用session-->
<h2>当前用户:{{ request.session.user_info.nickname }}</h2>
<ul>
{% for i in userList %}
<li> {{ i.nickname }}</li>
{% endfor %}
</ul>
</body>
</html>
session相关操作:
#与字典相似
request.session.keys()
request.session.values()
request.session.items()
request.session.session_key #用户的随机字符串
request.session.delete('session_key') #删除当前用户的session
request.session.exists('session_key') #检查当前用户的随机字符串是否在数据库中
request.session.flush() #删除所有session
request.session.clear_expired() #清除过期的session
request.session.set_expiry(value) #设置超时时间,默认两周,单位(秒)
session相关设置:
参数 | 意义 | 默认 |
---|---|---|
SESSION_COOKIE_NAME | 保存在浏览器中的key | ‘sessionid’ |
SESSION_COOKIE_PATH | session的cookie路径 | “\” |
SESSION_COOKIE_DOMAIN | session的cookie保存的域名 | None |
SESSION_COOKIE_SECURE | 是否使用Https传输cookie | False |
SESSION_COOKIE_HTTPONLY | 是否仅使用Http传输cookie | True |
SESSION_EXPIRE_AT_BROWSER_CLOSE | 字面意思 | |
SESSION_SAVE_EVERY_REQUEST | 是否每次刷新浏览器都更新默认时间 | False |
上述参数均可在setting文件中更改设置
setting.py
SESSION_COOKIE_NAME = 'sessionid'
SESSION_COOKIE_PATH = '/'
SESSION_COOKIE_DOMAIN = None
SESSION_COOKIE_SECURE = False
SESSION_COOKIE_HTTPONLY = True
SESSION_EXPIRE_AT_BROWSER_CLOSE = False
SESSION_SAVE_EVERY_REQUEST = False
#默认
SESSION_ENGINE='django.contrib.sessions.backends.db' #session引擎设置 表示将session_key放入数据库
SESSION_ENGINE= 'django.contrib.sessions.backends.file'#将session存入文件
SESSION_FILE_PATH='\XX\XX'
#
SESSION_ENGINE='django.contrib.sessions.backends.cache'#放缓存
SESSION_ENGINE='django.contrib.sessions.backends.cache_db'#缓存+数据库,缓存找不着就找数据库