COOKIE,SESSION:
cookie原理:由服务器产生内容,浏览器收到请求后保存在客户端本地,当浏览器再次访问时
浏览器会自动带上cookie,这样服务器就能通过cookie的内容来判断是谁,在一定程度解决
HTTP协议无法保持状态,但是最大支持4096个字节。
获取cookie: request.COOKIE['key']
设置cookie:
# rep = render(request,"user_center.html")
rep = redirect("/myapp/base_manager")
# rep.set_cookie("username",name,max_age=10,expires=t,path="/")
rep.set_cookie(key,value,max_age=None,expires=None,path='/',domain=None,secure=False,httponly=False)
key:键 value:值 max_age:超长时间 expires:超长时间 path:cookie生效时间,浏览器只会把cookie回传给带有该路劲的页面
这样可以避免cookie传给站点中的其他的应用
domain: cookie生效的域名
secure=False, 如果设置为True,浏览器将通过HTTP来回传cookie
httponly, 只能HTTP协议传输,无法为javascript获取(但是可以通过伪造浏览器请求来获取)
删除cookie:
response.delete_cookie("cookie_key",path="/",domain=name)
SESSION原理:由服务器产生内容,保存在服务器端
session的好处:客户端只有cookie的值,但是始终没有用户的信息,
session依赖于cookie,cookie保存在浏览器,session保存在服务器端。
1.设置session
request.session['is_login'] = True
2.获取session
request.session.get('is_login',False)
login = request.session['is_login']
3.删除session
del request.session['session_name']
4.检查是否操作session值
if 'session_name' is request.session
5.pop(key)
fav_color = request.session.pop('fav_color')
6.keys()
7.items()
8.setdefault()
9.flush() 删除当前的会话数据并删除会话的cookies,
这用于确保前面的会话数据不可以再次被用户的浏览器访问时logout()
10.删除当前用户所有失效日期小于当前的数据
request.session.clear_expired()
Django 内置用户认证:
1.在后台用request.user.is_authenticated()判断用户是否已经登录,如果true则可以向前台展示request.user.name
2.new_user = User.objects.create_user(username=username, password=password,email=email).save() 创建用户
3.@login_required # 登入装饰器
4.user.check_password(old_password): #检查密码是否正确
5.user.set_password(new_password).save() #修改密码
6.
注册
def sign_up(request):
state = None
if request.method == 'POST':
password = request.POST.get('password', '')
repeat_password = request.POST.get('repeat_password', '')
email=request.POST.get('email', '')
username = request.POST.get('username', '')
if User.objects.filter(username=username):
state = 'user_exist'
else:
new_user = User.objects.create_user(username=username, password=password,email=email)
new_user.save() #注册用户
return redirect('/book/')
content = {
'state': state,
'user': None,
}
return render(request, 'sign_up.html', content)
修改密码:
@login_required # 登入装饰器
def set_password(request):
user = request.user
state = None
if request.method == 'POST':
old_password = request.POST.get('old_password', '')
new_password = request.POST.get('new_password', '')
repeat_password = request.POST.get('repeat_password', '')
if user.check_password(old_password): #检查密码是否正确
if not new_password:
state = 'empty'
elif new_password != repeat_password:
state = 'repeat_error'
else:
user.set_password(new_password) #修改密码
user.save()
return redirect("/log_in/")
else:
state = 'password_error'
content = {
'user': user,
'state': state,
}
return render(request, 'set_password.html', content)
登入:
from django.contrib.auth import authenticate, login
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password) #检查用户和密码是否正确
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
...
注销:
from django.contrib.auth import logout
def logout_view(request):
logout(request)
# Redirect to a success page.