zoukankan      html  css  js  c++  java
  • django 往session写信息

    def main(req):
              try:
                 req.session['username']
                 return render_to_response('main.html')
              except:
                 try:
                     a=req.POST['username']
                     if (check_user(req.POST['username'],req.POST['password'])== 1):
                        req.session['username']=req.POST['username']
                        return render_to_response('main.html')
                     else:
                      response = "用户名或密码错误"
                      return HttpResponse(response)
                      #return redirect('/index/')
                 except:
                   return redirect('/index/')
    
    
    
      req.session['username']=req.POST['username']
      
      其中的 req.session['username']=req.POST['username'] 执行了以下几步:
    
    1.生成随机的sessionID字符串
    
    2.将sessionID和用户的信息在数据库中保存为一个键值对
    
    3.通过cookie将sessionID保存在客户端上
    
    这时候通过用户再次向服务器发送请求时服务器就可以通过请求中的sessionID判断用户的信息了,从而达到保存登录状态的要求。
    
  • 相关阅读:
    IO流
    异常,File,递归,IO流
    Collection接口 map
    使用canvas画出的时钟
    js对象2
    js对象
    js 猜数游戏、斗地主发牌、伪数字
    js函数2
    js函数
    js矩形,数组,杨辉三角
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349136.html
Copyright © 2011-2022 走看看