Cookie和Session是用来在Web应用程序中跟踪用户会话数据的两种常用技术。
- 虽然cookie是服务端告诉客户端浏览器需要保存内容
- 但是客户端浏览器可以选择拒绝保存
- 如果禁止自动保存cookie
- 那么只要是需要登录的网站都没办法正常登录了
- return HttpResponse()
- return render()
- return redirect()
obj = HttpResponse("ok")
obj.set_cookie('k','v')
def login(request, *args, **kwargs):
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
if username == "dream" and password == "521":
obj = HttpResponse("ok")
obj.set_cookie('sign', 'user')
return obj
else:
return redirect('/login/')
return render(request, 'login.html')
def home(request, *args, **kwargs):
sign = request.COOKIES.get('sign')
if sign and sign == 'user':
return HttpResponse("这是home页面")
else:
return redirect('/login/')
request.COOKIES.get('k')
obj.set_cookie('sign', 'user', expires=3)
obj.set_cookie('sign', 'user', max_age=3)
#设置超时时间3s到期
max_age
设置超时时间,以秒为单位
expiress
设置超时时间 针对IE浏览器使用,以秒为单位
def logout(request, *args, **kwargs):
obj = redirect('/home/')
# 设置超时时间 3s 到期
obj.delete_cookie('sign')
return obj
def login(request, *args, **kwargs):
# next_url = request.get_full_path()
# print(next_url) # /login/?next_url=/home/
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
if username == "dream" and password == "521":
next_url = request.GET.get('next_url')
# print(next_url) # /home/
obj = redirect(next_url)
obj.set_cookie('sign', 'user')
return obj
else:
return redirect('/login/')
return render(request, 'login.html')
def login_auth(func):
def inner(request, *args, **kwargs):
# print(request.path_info) # /home/
# print(request.get_full_path()) # /home/?username=111
next_url = request.get_full_path() # /home/
# print(next_url)# /home/
sign = request.COOKIES.get('sign')
if sign and sign == 'user':
res = func(request, *args, **kwargs)
return res
else:
return redirect(f'/login/?next_url={next_url}')
return inner
@login_auth
def home(request, *args, **kwargs):
return HttpResponse("这是home页面")
# def home(request, *args, **kwargs):
# sign = request.COOKIES.get('sign')
# if sign and sign == 'user':
# return HttpResponse("这是home页面")
# else:
# return redirect('/login/')
@login_auth
def index(request, *args, **kwargs):
return HttpResponse("这是index页面")
- session数据是保存在服务端的,给客户端返回的是一个随机字符串
- sessionid:随机字符串
request.session['sign'] = 'user'
sign = request.session.get('sign')
request.session['sign'] = 'user'
# 如果是数字的话就是指定 s shu
# request.session.set_expiry(3)
# 0 就是关闭浏览器后自动清除浏览器的sessionid
request.session.set_expiry(0)
# 删除session方式一
# request.session.delete()
# 把浏览器和数据库里面的session全部清除掉
request.session.flush()
request.session.delete()
:只删除服务端的
request.session.flush()
:服务端和客户端都删除
session基于数据库表才能使用的
必须先迁移数据库,生成 django_session 表
session只对当次登录有效
主动清除浏览器中本地存在的session
验签发现,没有sessionid就会自动生成新的session
django_sessoin
表中的数据条数取决于浏览器
同一个计算机(IP地址)上同一个浏览器只会有一条数据生效
同一个计算机(IP地址)上多个浏览器会有多个数据生效
当session过期的时候,可能会出现多条数据对应一个浏览器
但是这些数据不会持久化存储,会被定时清理掉,可以手动清除也可以代码清除
目的是为了节省服务器数据库资源
def login(request, *args, **kwargs):
# next_url = request.get_full_path()
# print(next_url) # /login/?next_url=/home/
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
if username == "dream" and password == "521":
# next_url = request.GET.get('next_url')
# print(next_url) # /home/
request.session['sign'] = 'user'
obj = redirect('/home/')
# 设置过期时间
# obj.set_cookie('sign', 'user', expires=3)
# obj.set_cookie('sign', 'user', max_age=3)
return obj
else:
return redirect('/login/')
return render(request, 'login.html')
def login_auth(func):
def inner(request, *args, **kwargs):
# print(request.path_info) # /home/
# print(request.get_full_path()) # /home/?username=111
next_url = request.get_full_path() # /home/
# print(next_url)# /home/
sign = request.session.get('sign')
# print(sign) # user
if sign and sign == 'user':
res = func(request, *args, **kwargs)
return res
else:
return redirect(f'/login/?next_url={next_url}')
return inner
@login_auth
def home(request, *args, **kwargs):
return HttpResponse("这是home页面")
from django.utils.decorators import method_decorator
@method_decorator(login_auth)
def get(self, request, *args, **kwargs):
return HttpResponse("这是home页面")
def post(self):
# @method_decorator(login_auth, name='get')
# @method_decorator(login_auth, name='post')
class UserView(View):
@method_decorator(login_auth)
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
if request.method.lower() in self.http_method_names:
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
else:
handler = self.http_method_not_allowed
return handler(request, *args, **kwargs)