博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dj cookie与session 2
阅读量:6275 次
发布时间:2019-06-22

本文共 6331 字,大约阅读时间需要 21 分钟。

 

 

def login_session(request):    if request.method == "POST":        user = request.POST.get("user")        pwd = request.POST.get("pwd")        user = UserInfo.objects.filter(user=user, pwd=pwd).first()        if user:            import datetime            now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")            request.session["is_login"] = True            request.session["username"] = user.user            request.session["last_visit_time"] = now            '''            if request.COOKIE.get("sessionid"):                更新                 在django—session表中创建一条记录:                   session-key                                     session-data                   ltv8zy1kh5lxj1if1fcs2pqwodumr45t                  更新数据            else:                1 生成随机字符串   ltv8zy1kh5lxj1if1fcs2pqwodumr45t                2 response.set_cookie("sessionid",ltv8zy1kh5lxj1if1fcs2pqwodumr45t)                3 在django—session表中创建一条记录:                   session-key                                     session-data                   ltv8zy1kh5lxj1if1fcs2pqwodumr45t       {"is_login":True,"username":"yuan"}            '''            return HttpResponse("登录成功!")    return render(request, "login.html")
session 设置及原理

 

def index_session(request):    print("is_login:", request.session.get("is_login"))    '''    1  request.COOKIE.get("session")  #  ltv8zy1kh5lxj1if1fcs2pqwodumr45t    2  django-session表中过滤纪录:       在django—session表中创建一条记录:               session-key                                   session-data               ltv8zy1kh5lxj1if1fcs2pqwodumr45t       {"is_login":True,"username":"yuan"}       obj=django—session.objects .filter(session-key=ltv8zy1kh5lxj1if1fcs2pqwodumr45t).first()    3 obj.session-data.get("is_login")    '''    is_login = request.session.get("is_login")    if not is_login:        return redirect("/login_session/")    username = request.session.get("username")    last_visit_time = request.session.get("last_visit_time")    return render(request, "index.html", {"username": username, "last_visit_time": last_visit_time})def logout(request):    # del request.session["is_login"]    request.session.flush()    '''    1 randon_str=request.COOKIE.get("sessionid")    2 django-session.objects.filter(session-key=randon_str).delete()    3 response.delete_cookie("sessionid",randon_str)    '''    return redirect("/login/")
获取 session 与删除

 

from django.shortcuts import render, HttpResponse, redirectfrom app01.models import UserInfodef login(request):    if request.method == "POST":        user = request.POST.get("user")        pwd = request.POST.get("pwd")        user = UserInfo.objects.filter(user=user, pwd=pwd).first()        if user:            # 登陆成功            '''            响应体:            return HttpResponse()            return render()            return redirect()            '''            response = HttpResponse("登录成功!")            # response.set_cookie("is_login",True,max_age=15)            response.set_cookie("is_login", True)            import datetime            # date=datetime.datetime(year=2019,month=5,day=29,hour=14,minute=34)            # response.set_cookie("username",user.user,expires=date)            response.set_cookie("username", user.user, path="/index/")            return response    return render(request, "login.html")def index(request):    print("index:", request.COOKIES)    is_login = request.COOKIES.get("is_login")    if is_login:        username = request.COOKIES.get("username")        import datetime        now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")        last_time = request.COOKIES.get("last_visit_time", "")        response = render(request, "index.html", {"username": username, "last_time": last_time})        response.set_cookie("last_visit_time", now)        return response    else:        return redirect("/login/")def test(request):    print("test:", request.COOKIES)    return HttpResponse("test!")def login_session(request):    if request.method == "POST":        user = request.POST.get("user")        pwd = request.POST.get("pwd")        user = UserInfo.objects.filter(user=user, pwd=pwd).first()        if user:            import datetime            now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")            request.session["is_login"] = True            request.session["username"] = user.user            request.session["last_visit_time"] = now            '''            if request.COOKIE.get("sessionid"):                更新                 在django—session表中创建一条记录:                   session-key                                     session-data                   ltv8zy1kh5lxj1if1fcs2pqwodumr45t                  更新数据            else:                1 生成随机字符串   ltv8zy1kh5lxj1if1fcs2pqwodumr45t                2 response.set_cookie("sessionid",ltv8zy1kh5lxj1if1fcs2pqwodumr45t)                3 在django—session表中创建一条记录:                   session-key                                     session-data                   ltv8zy1kh5lxj1if1fcs2pqwodumr45t       {"is_login":True,"username":"yuan"}            '''            return HttpResponse("登录成功!")    return render(request, "login.html")def index_session(request):    print("is_login:", request.session.get("is_login"))    '''    1  request.COOKIE.get("session")  #  ltv8zy1kh5lxj1if1fcs2pqwodumr45t    2  django-session表中过滤纪录:       在django—session表中创建一条记录:               session-key                                   session-data               ltv8zy1kh5lxj1if1fcs2pqwodumr45t       {"is_login":True,"username":"yuan"}       obj=django—session.objects .filter(session-key=ltv8zy1kh5lxj1if1fcs2pqwodumr45t).first()    3 obj.session-data.get("is_login")    '''    is_login = request.session.get("is_login")    if not is_login:        return redirect("/login_session/")    username = request.session.get("username")    last_visit_time = request.session.get("last_visit_time")    return render(request, "index.html", {"username": username, "last_visit_time": last_visit_time})def logout(request):    # del request.session["is_login"]    request.session.flush()    '''    1 randon_str=request.COOKIE.get("sessionid")    2 django-session.objects.filter(session-key=randon_str).delete()    3 response.delete_cookie("sessionid",randon_str)    '''    return redirect("/login/")
view all

 

httpresponse 对象设置cookie

转载于:https://www.cnblogs.com/wenyule/p/10519577.html

你可能感兴趣的文章
第2课:通过案例对SparkStreaming 透彻理解三板斧之二:解密SparkStreaming运行机制和架构...
查看>>
IOS开发—App 在 IOS 8 的simulator运行时,定位卡死bug解决
查看>>
windows 密钥登陆 linux
查看>>
IOS 录制视频
查看>>
limit检查
查看>>
Android Things 简介
查看>>
菜鸟学Linux 第049篇笔记 DNS log, zone, view
查看>>
菜鸟学Linux 第054篇笔记 建立加密的http
查看>>
ListView 的多选模式
查看>>
宏正自动科技发表新款8/16端口双滑轨LCD KVM多电脑切换器
查看>>
解决 Missing GL version
查看>>
VS 编译链接错误集锦
查看>>
Dns域名服务器之,ACL ,转发域及子域授权的基本配置
查看>>
Android权限列表
查看>>
Linux中的网络监控命令
查看>>
360项目-07
查看>>
使用Nginx进行TCP/UDP端口转发
查看>>
读书笔记2(Effective java)
查看>>
[bat]批量替换文件内容
查看>>
Java代码到字节码——第一部分
查看>>