Django-cookie与session操作
添加cookie:
def login(req):
if req.method=="POST":
uf = UserInfoForm(req.POST)
if uf.is_valid():
username = uf.cleaned_data["username"]
password = uf.cleaned_data["password"]
print username,password
users = UserInfo.objects.filter(username=username,password=password)
if users:
response = HttpResponseRedirect("/index/")
response.set_cookie("username",username,3600)
return response
else:
return HttpResponseRedirect("/login")
# return HttpResponseRedirect()
else:
uf = UserInfoForm()
return render_to_response("login.html",{"uf":uf})
获得cookie:
def index(req):
username = req.COOKIES.get("username","")
return render_to_response("index.html",{"username":username})
删除cookie:
Response.delete_cookie("username")
添加session:
def sesion(req):
if req.method == "POST":
uf = UserInfoForm(req.POST)
if uf.is_valid():
username = uf.cleaned_data["username"]
req.session["username"] = username
return HttpResponseRedirect("/index/")
else:
uf = UserInfoForm()
return render_to_response("LoadFile.html",{"uf":uf})
获取session:
def index(req):
username = req.session.get("username","")
return render_to_response("index.html",{"username":username})
删除session:
del req.session['username']
Django-cookie与session操作的更多相关文章
- [py][mx]django的cookie和session操作-7天免登录
浏览器同源策略(same-origin policy) csrf攻击防御核心点总结 django的cookie和session操作-7天免登录 flask操作cookie&django的see ...
- Django Cookie于Session
一.Cookie与Session由来 因为Http协议的特性,每一次来自用户浏览器的请求都是无状态且独立的,通俗地说,就是无法保存用户状态,后台服务器根本就不知道当前请求和以前及以后请求是否来自同一用 ...
- Django中的Cookie和Session操作以及CBV
1.Cookie 平常我们在浏览网页的时候,在需要输入密码的地方,如果已经登陆了一次,并且时间间隔比较近的话,是不需要登陆的,为什么了?这就是Cookie的作用. Cookie(或Cookies)指某 ...
- Python Web框架篇:Django cookie和session
part 1 概念 在Django里面,cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话. 两者最大的区别是cookie的信息是存放在浏览器客户端的,而sessio ...
- python Django cookie和session
在一个会话的多个请求中共享数据,这就是会话跟踪技术.例如在一个会话中的请求如下: 请求银行主页: 请求登录(请求参数是用户名和密码): 请求转账(请求参数与转账相关的数据): 请求信誉卡还款(请求参 ...
- django cookie与session组件
本文目录 1 会话跟踪技术 2 cookie介绍 Django中操作Cookie Session Django中Session相关方法 Django中的Session配置 CBV中加装饰器 回到目录 ...
- 事务、cookie、session操作
事务 import os if __name__ == '__main__': os.environ.setdefault('DJANGO_SETTINGS_MODULE','BMS.settings ...
- Django - Cookie、Session、自定义分页和Django分页器
2. 今日内容 https://www.cnblogs.com/liwenzhou/p/8343243.html 1. Cookie和Session 1. Cookie 服务端: 1. 生成字符串 2 ...
- Python学习---django下的Session操作 180205
和Cookie一样,都是用来进行用户认证.不同的是,Cookie可以吧明文/密文的信息都会KV返回给客户段,但是session可以吧用户的Value[敏感信息]保存在服务器端,安全. Django中默 ...
- django cookie and session
cookie和session 1.cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因此cookie就是在这样一个场景下诞生. cookie的工作 ...
随机推荐
- 浅析Java开发模式—Model1、Model2和三层
"解耦"的思想一直是我们倡导的,但在实际项目中怎样去做?这是需要我们去好好思考的.下面以Model1.Model2.三层为切入点,对比下去了解解耦的思想. Model1 使用JSP ...
- codility MinAbsSum
For a given array A of N integers and a sequence S of N integers from the set {−1, 1}, we define val ...
- 关于EditText的android:maxLength属性的注意事项
一直以为在xml布局文件中对EditText添加 android:maxLength="30";属性是控制EditText字符数的.想当然的以为一个中文占2个字符,一个英文占1个字 ...
- 二分查找法(binary search)
二分查找法:一种在有序列表中查找某个值的算法,它每次都将待查找的空间分为两半,在其中一般继续查找. 使用二分查找的前提是:已经排序好的列表.否则,sum对其查找的结果不做保证. 代码实现: // 使用 ...
- MySQL 字符编码问题详细解释
http://www.codesoil.net/tag/charset Character Set Problem in PHP + MySQL4.1+ 和许多人一样,我也是在转移blog时才发现这个 ...
- 9.22 NOIP模拟题
吉林省信息学奥赛 2017 冬令营 ...
- [Swift通天遁地]三、手势与图表-(6)创建包含三条折线的线性图表
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [转]Linux中set,env和export这三个命令的区别
转自:http://www.2cto.com/os/201306/223758.html Linux中set,env和export这三个命令的区别 set命令显示当前shell的变量,包括当前用户 ...
- C#图片辅助类,形成缩略图
完善一下别人的方法,成自己好用的工具 using System.Drawing; using System.Drawing.Imaging; namespace GXNUQzzx.Tools.Util ...
- fcc 响应式框架Bootstrap 练习3
class="container-fluid"控制宽度100% <div class="container-fluid"> <h3 class ...