首页 > 其他 > 详细

django 学习-17 Django会话Session

时间:2016-01-17 23:00:16      阅读:329      评论:0      收藏:0      [点我收藏+]

session   类似于字典的一个对象,是可读可写的

跟cookie的变化不大而且还显得简单

在这里我只说一下改变的地方

1、vim blog/views.py

 if uf.is_valid():
                        username = uf.cleaned_data[‘username‘]
                        password = uf.cleaned_data[‘password‘]
                        users = User.objects.filter(username__exact=username,password__exact=password)

                        req.session[‘username‘] = username          直接调取

         if users:
                                response =  HttpResponseRedirect(‘/index/‘)
                                response.set_cookie(‘username‘,username,3600)
                                return response
                        else:
                                return HttpResponseRedirect(‘/login/‘)
        else:
                uf = UserForm()
        return render_to_response(‘login.html‘,{‘uf‘:uf})

def index(req):
        username = req.session.get(‘username‘,‘ ‘)                        登陆上去网页会有一条session记录
        return render_to_response(‘index.html‘, {‘username‘:username})
def logout(req):
#       response = HttpResponse(‘logout‘)
#       response.delete_cookie(‘username‘)
        del req.session[‘username‘]                            直接删除,
        return HttpResponse(‘logout ok‘)

django 学习-17 Django会话Session

原文:http://www.cnblogs.com/Icanflyssj/p/5137930.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!