码迷,mamicode.com
首页 > 其他好文 > 详细

完成登录功能,用session记住用户名

时间:2017-11-21 23:40:48      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:else   dir   har   pre   ack   首页   style   render   dex   

登录功能完成:

  1. js:设置return
  2. html:设置
    1. form
    2. input
  3. py:
    1. @app.route设置methods
    2. GET
    3. POST
      1. 读取表单数据
      2. 查询数据库
        1. 用户名密码对:
          1. 记住用户名
          2. 跳转到首页
        2. 用户名密码不对:
          1. 提示相应错误。

session:

  1. 从`flask`中导入`session`
  2. 设置`SECRET_KEY`
  3. 操作字典一样操作`session`:增加用户名`session[‘username‘]=`username

 

登录功能完成:

 1 @app.route(/login/, methods=[GET, POST])
 2 def login():
 3     if request.method == GET:
 4         return render_template("login.html")
 5     else:
 6         username = request.form.get(username)
 7         password = request.form.get(password)
 8         user = User.query.filter(User.username == username).first()
 9         if user:
10             if user.password == password:
11                 return redirect(url_for(myweb))
12             else:
13                 return 密码错误
14         else:
15             return 用户名不存在
 1  <div class="logo_box">
 2         <h3 style="text-align: center">欢迎你</h3>
 3         <form action="{{ url_for(‘login‘) }}" method="post">
 4             <div class="input_outer">
 5                 <span class="u_user"></span>
 6                 <input id="uname" class="text" style="color: wheat" type="text" placeholder="请输入账号" name="username">
 7             </div>
 8             <div class="input_outer">
 9                 <span class="us_uer"></span>
10                 <input id="upass" class="text" style="color:wheat ; position:absolute; z-index:100;"
11                        value="" type="password" placeholder="请输入密码" name="password">
12             </div>
13             <div class="errorText" id="error_box" style="color: red"><br></div>
14             <div>
15                 <button onclick="return fnLogin()" class="lb1" style="color:black">登录</button>
16             </div>
17         </form>
18     </div>

 

session:

 1 import os
 2 DEBUG = True
 3 
 4 SECRET_KEY = os.urandom(24)
 5 
 6 DIALECT = mysql
 7 DRIVER = mysqldb
 8 USERNAME = root
 9 PASSWORD = ROOT
10 HOST = 127.0.0.1
11 DATABASE = mytest
12 
13 
14 SQLALCHEMY_DATABASE_URI = mysql+pymysql://root:@127.0.0.1:3306/mytest?charset=utf8
15 SQLALCHEMY_TRACK_MODIFICATIONS = False

 

完成登录功能,用session记住用户名

标签:else   dir   har   pre   ack   首页   style   render   dex   

原文地址:http://www.cnblogs.com/-064/p/7875615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!