zoukankan      html  css  js  c++  java
  • django 学习-17 Django会话Session

    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')

  • 相关阅读:
    dotnet Framework 源代码 类库的意思
    dotnet Framework 源代码 类库的意思
    dotnet Framework 源代码 · Ink
    dotnet Framework 源代码 · Ink
    dotnet core 用值初始化整个数组
    dotnet core 用值初始化整个数组
    dotnet core 隐藏控制台
    dotnet core 隐藏控制台
    PHP mt_rand() 函数
    PHP mt_getrandmax() 函数
  • 原文地址:https://www.cnblogs.com/Icanflyssj/p/5137930.html
Copyright © 2011-2022 走看看