一、基础
cookie保存在浏览器本地,格式:iii="8jblpb9g2l5c9xvofhv5pt8c0v967z5t"
session保存在服务器端。格式:{"cookie值":{"isLogin":True,"username":"xxx"},cookie:{}}
二、流程
- 服务器生成一段字符串
- 把字符串发送给客户端浏览器,同时将字符串当做key放置session信息
- 在用户的session对应的value(字典)里设置任意值
三、错误解决
报错:no such table: django_session
原因:
Django的session是放在数据库里,要先初始化表。
解决:
项目目录路径> python manage.py migrate
四、实例
1、需求
订单页面未登录不能访问,直接跳转到登录页面。
2、登录页面
app01/views.py
1
2
3
4
5
6
7
8
|
def login(request):
if request.method = = ‘POST‘ :
username = request.POST.get( ‘username‘ )
pwd = request.POST.get( ‘pwd‘ )
if username = = ‘user1‘ and pwd = = ‘123‘ :
request.session[ ‘is_login‘ ] = True
return redirect( ‘/order/‘ )
return render(request, ‘login.html‘ )
|
urls.py
1
2
3
4
5
|
from app01 import views
urlpatterns = [
url(r ‘^admin/‘ , admin.site.urls),
url(r ‘^login/$‘ , views.login),
]
|
templates/login.html
1
2
3
4
5
6
7
|
< body >
< form action = "/login/" method = "post" >
< input type = "text" name = "username" >
< input type = "password" name = "pwd" >
< input type = "submit" value = "submit" >
</ form >
</ body >
|
4、订单页面
app01/views.py
1
2
3
4
5
6
|
def order(request):
is_login = request.session.get( ‘is_login‘ , False ) #False是默认值,不设置会报错
if is_login:
return HttpResponse( ‘order‘ )
else :
return redirect( ‘/login/‘ )
|
urls.py
1
2
3
4
5
6
|
from app01 import views
urlpatterns = [
url(r ‘^admin/‘ , admin.site.urls),
url(r ‘^order/$‘ , views.order),
url(r ‘^login/$‘ , views.login),
]
|
5、浏览器
6、数据库
7、扩展:订单页面右上角显示登录用户
还可以定义其它值,如username,登录成功的时候就设置上,然后页面可以根据不同的用户显示不同的数据。
views.py
1
2
3
4
5
6
7
8
9
|
def login(request):
if request.method = = ‘POST‘ :
username = request.POST.get( ‘username‘ )
pwd = request.POST.get( ‘pwd‘ )
if username = = ‘user1‘ and pwd = = ‘123‘ :
request.session[ ‘is_login‘ ] = True
request.session[ ‘username‘ ] = ‘user1‘
return redirect( ‘/order/‘ )
return render(request, ‘login.html‘ )
|
views.py
1
2
3
4
5
6
7
|
def order(request):
is_login = request.session.get( ‘is_login‘ , False ) #False是默认值,不设置会报错
if is_login:
username = request.session.get( ‘username‘ , False )
return render(request, ‘order.html‘ ,{ ‘username‘ :username})
else :
return redirect( ‘/login/‘ )
|
oreder.html
1
2
3
|
< body >
{{username}}
</ body >
|
8、注销?
order.html
1
2
3
4
|
< body >
{{ username }}
< a href = "/logout/" >注销</ a > #加入注销跳转到logout函数处理
</ body >
|
urls.py
1
2
3
4
5
6
7
8
|
from app01 import views
urlpatterns = [
url(r ‘^admin/‘ , admin.site.urls),
url(r ‘^$‘ , views.index),
url(r ‘^order/$‘ , views.order),
url(r ‘^login/$‘ , views.login),
url(r ‘^logout/$‘ , views.logout), #加一个logout
]
|
views.py
1
2
3
|
def logout(request):
del request.session[ ‘is_login‘ ]
return redirect( ‘/login/‘ )
|
五、cookie超时时间
settings.py
1
|
SESSION_COOKIE_AGE = 5 #单位:秒
|
session也有
1
2
3
4
5
|
request.session.set_expiry(value)
* 如果value是个整数,session会在些秒数后失效。
* 如果value是个datatime或timedelta,session就会在这个时间后失效。
* 如果value是 0 ,用户关闭浏览器session就会失效。
* 如果value是 None ,session会依赖全局session失效策略。
|
更多
https://docs.djangoproject.com/en/1.9/topics/http/sessions/
http://docs.30c.org/djangobook2/chapter14/
https://docs.djangoproject.com/en/1.9/ref/settings/#settings-sessions
来自为知笔记(Wiz)
Django--cookie&session
时间: 2024-11-08 20:24:18