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, redirect

from app01.models import UserInfo

def 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

dj cookie与session 2的更多相关文章

  1. Cookie和Session总结

    Cookie概述     Cookie是什么?       Cookie是一小段文本信息,伴随着用户请求和页面在Web服务器和浏览器之间传递.Cookie包含每次用户访问站点时Web应用程序都可以读取 ...

  2. Cookie和Session的总结

    1.开篇 在之前学习这一段的时候我一直有点没弄清楚,其实对Session这块的理解还可以,但是Cookie感觉始终还是欠缺点火候.之后的很长一段时间都基本上很少用Cookie了,渐渐的也淡忘了这一块的 ...

  3. java的会话管理:Cookie和Session

    java的会话管理:Cookie和Session 1.什么是会话 此处的是指客户端(浏览器)和服务端之间的数据传输.例如用户登录,购物车等 会话管理就是管理浏览器客户端和服务端之间会话过程产生的会话数 ...

  4. Cookie和Session的那些事儿

    Cookie和Session都是为了保持用户的访问状态,一方面为了方便业务实现,另一方面为了简化服务端的程序设计,提高访问性能.Cookie是客户端(也就是浏览器端)的技术,设置了Cookie之后,每 ...

  5. django的cookie和session以及内置信号、缓存

    cookie和session cookie和session的作用: cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话.两者最大的区别是cookie的信息是存放在浏览 ...

  6. Cookie和Session的区别

    前言 HTTP是一种无状态的协议,为了分辨链接是谁发起的,就需要我们自己去解决这个问题.不然有些情况下即使是同一个网站我们每打开一个页面也都要登录一下.而Session和Cookie就是为解决这个问题 ...

  7. 本地数据Store。Cookie,Session,Cache的理解。Timer类主要用于定时性、周期性任务 的触发。刷新Store,Panel

    本地数据Store var monthStore = Ext.create('Ext.data.Store', { storeId : 'monthStore', autoLoad : false, ...

  8. Cookie与Session

    再说Cookie与Session之前,先要了解一下http协议. 何为http协议: http协议即超文本传输协议,一种基于浏览器请求与服务器响应的协议,该协议主要的特点就是它是一种无状态的协议(只针 ...

  9. 【转】Cookie和Session区别和联系详解

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

随机推荐

  1. js中json知识点

    首先,json是一种数据格式,而不能说是一种对象(object).这一点是非常重要的. 起源是不同的语言中数据对象的形式是不一样的,我们为了在不同的语言中传递数据,发明了一种json格式用于消除这种差 ...

  2. 同种类型不同名字的变量在for循环中操作

    InfoViewController * info = [[InfoViewController alloc] init]; HeroViewController * hero = [[HeroVie ...

  3. Activity 的启动过程深入学习

    手机应用也是一个app,每一个应用的icon都罗列在Launcher上,点击icon触发onItemClick事件. 我们要启动「淘宝」这个App,首先我们要在清单文件定义默认启动的Activity信 ...

  4. git--(3)分支 合并

    git branch test //新建分支 git branch //列出分支 git branch -r //列出远程分支 git branch -m | -M oldbranch newbran ...

  5. YII2中自定义用户认证模型,完成登陆和注册

    有些时候我们需要自已定义用户类,操作自已建的用户表,来完成登陆和注册功能. 用户表结构如下,当然可以根据自已的需要添加或删除: CREATE TABLE `tb_user` ( `id` int(11 ...

  6. Python ctypes.windll.user32() Examples

    Example 1 Project: OSPTF   Author: xSploited   File: mouselogger.py View Source Project 7 votes def ...

  7. 使用clear来清除localStorage保存对象的全部数据

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. BZOJ1217或洛谷2279 [HNOI2003]消防局的设立

    BZOJ原题链接 洛谷原题链接 该题有两种做法,树形\(DP\)和贪心. 先讲贪心. 先将所有点按深度从大到小排序,然后从大到小依次取出点,若已经被覆盖则跳过,否则就在它的祖父点建立消防站. 考虑如何 ...

  9. Luogu 2279 [HNOI2003]消防局的设立 - 贪心

    Description 给定一棵树形图, 建若干个消防站, 消防站能够覆盖到距离不超过2的点, 求最少需要建几个消防站才能覆盖所有点 Solution 从深度最深的点开始, 在它的爷爷节点上建, 每建 ...

  10. post方式发送请求报文

    $url="http://www.test.com/04_demo_weather.php?id=5"; $ci=curl_init($url); curl_setopt($ci, ...