网站制作报价维持地建网络,挣钱最快的小游戏,wordpress国内最好的主题,手机百度云转换wordpresscookie和session
发展史 一开始,只有一个页面#xff0c;没有登录功能#xff0c;大家看到东西都一样。 时代发展#xff0c;出现了需要登录注册的网站#xff0c;要有一门技术存储我们的登录信息#xff0c;于是cookie诞生了。 cookie: - 存储形式#xff1a;k:v键值对…cookie和session
发展史 一开始,只有一个页面没有登录功能大家看到东西都一样。 时代发展出现了需要登录注册的网站要有一门技术存储我们的登录信息于是cookie诞生了。 cookie: - 存储形式k:v键值对 - 存储位置客户端 - 不安全信息可能会泄露 时代再度发展需要有一门新的安全的技术于是有了session。 session: - 标识符来表示我是当前用户加密出来的数据 - 对敏感信息进行加密处理 - 存储服务端 - 标识符配合上你的加密串 - 把我的标识符 字符串全给客户端 - 客户端存储格式 - session_id:返回回来的表示符加密串 django操作cookies 设置cookie
def login(request, *args, **kwargs):if request.method POST:username request.POST.get(username)password request.POST.get(password)if username kevin and password 123:obj HttpResponse(ok)obj.set_cookie(sign, user)return objelse:return redirect(/login/)return render(request, login.html) 取值cookie验证
def home(request, *args, **kwargs):sign request.COOKIES.get(sign)if sign and sign user:return HttpResponse(这是home页面)else:return redirect(/login/) 设置过期时间
obj.set_cookie(sign, user, expires3)
obj.set_cookie(sign, user, max_age3) 删除cookie
def logout(request, *args, **kwargs):obj redirect(/home/)# 设置超时时间 5s 到期obj.delete_cookie(sign)return obj
django操作session 设置session
request.session[sign] user 取值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] userobj redirect(/home/)# 设置过期时间# obj.set_cookie(sign, user, expires3)# obj.set_cookie(sign, user, max_age3)return objelse: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/?username111next_url request.get_full_path() # /home/# print(next_url)# /home/sign request.session.get(sign)# print(sign) # userif sign and sign user:res func(request, *args, **kwargs)return reselse:return redirect(f/login/?next_url{next_url})
return inner
login_auth
def home(request, *args, **kwargs):return HttpResponse(这是home页面) 注 - session基于数据库表才能使用的 - 必须先迁移数据库生成 django_session 表 - session只对当次登录有效 - 主动清除浏览器中本地存在的session - 验签发现没有sessionid就会自动生成新的session - django_sessoin表中的数据条数取决于浏览器 - 同一个计算机(IP地址)上同一个浏览器只会有一条数据生效 - 同一个计算机(IP地址)上多个浏览器会有多个数据生效 - 当session过期的时候可能会出现多条数据对应一个浏览器 - 但是这些数据不会持久化存储会被定时清理掉可以手动清除也可以代码清除 - 目的是为了节省服务器数据库资源 设置session过期时间
request.session[sign] user
request.session.set_expiry(0) 删除cookie
# 删除session
request.session.delete()
# 把浏览器和数据库里面的session全部清除掉
request.session.flush()
CBV加装饰器的三种方法
from django.utils.decorators import method_decorator
# 方式二放在类视图上面 (放的装饰器函数,name指定你的视图函数里面的方法)
# method_decorator(login_auth, nameget)
# method_decorator(login_auth, namepost)
class UserView(View):# 方式三 dispactch 方法加装饰器 本视图函数内所有的视图都需要走装饰器method_decorator(login_auth)def dispatch(self, request, *args, **kwargs):# Try to dispatch to the right method; if a method doesnt exist,# defer to the error handler. Also defer to the error handler if the# request method isnt 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_allowedreturn handler(request, *args, **kwargs)# 方式一加载视图函数上面# method_decorator(login_auth)def get(self, request, *args, **kwargs):return HttpResponse(这是home页面)
def post(self):...