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

登录功能完成:

  1. js:设置return
  2. html:设置
    1. form
    2. input
    3. onclick="return fnLogin()"
  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
    {% extends'base.html' %}
        {% block title %}
        登录
        {% endblock %}
    {% block head %}
    
        <link rel="stylesheet"type="text/css"href="{{ url_for('static',filename='css/login.css') }}">
        <script src="{{ url_for('static',filename='js/login.js') }}"></script>
    {% endblock %}
    {% block main %}
    <div class="Outbox">
          <div class="box">
              <h2>欢迎来到博客园,代码改变世界!</h2>
              <h3>登录页面</h3>
            <form action="{{ url_for('login')}}" method="post">
            <div class="input_box">
               账号:<input id="umane"type="text"placeholder="请输入账号">
            </div>
            <div class="input_box">
               密码:<input id="upass"type="password"placeholder="请输入密码">
            </div>
              <div id="error_box"><br></div>
              <div class="input_box">
                  <button onclick="myLogin()">登录</button>
                   <button onclick=window.alert("请重新输入")>取消</button></div>
    
              </div>
    </div>
    {% endblock %}
    

      

    function myLogin() {
                var oUname = document.getElementById("umane");
                var oError = document.getElementById("error_box");
                 var oUpass = document.getElementById("upass");
                oError.innerHTML = "<br>"
         //oUname
            if (oUname.value.length>12 || oUname.value.length<6){
                oError.innerHTML = "用户名必须为6到12位数";
                isError=false;
                return isError;
             } else if((oUname.value.charCodeAt(0)>=48) && oUname.value.charCodeAt(0)<=57){
    
                oError.innerHTML = "首字母不能为数字";
                 isError=false;
                return isError;
            }else for(var i=0; i<oUname.value.length;i++)
            {
    
         if((oUname.value.charCodeAt(i)<48 || oUname.value.charCodeAt(i)>57)&&(oUname.value.charCodeAt(i)<97 || oUname.value.charCodeAt(i)>122 )){
                        oError.innerHTML = "用户名只能包含字母和数字";
                        isError=false;
                        return isError;
                    }
                }
    
    
                if(upass.value.length>12 || upass.value.length<6){
                    oError.innerHTML="密码为6到12位数";
                    isError=false;
                    return isError;
                }
                return isError;
                window.alert("已成功登陆")
    
            }
    

      

    from flask import Flask,render_template,request,redirect,url_for,session
    from flask_sqlalchemy import SQLAlchemy
    import config
    
    app = Flask(__name__)
    app.config.from_object(config)
    db=SQLAlchemy(app)
    
    class User(db.Model):
        __table__name = 'user'
        id = db.Column(db.Integer,primary_key=True,autoincrement=True)
        username = db.Column(db.String(20),nullable=False)
        password = db.Column(db.String(20),nullable=False)
        nickname = db.Column(db.String(50))
    
    #db.create_all()
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    @app.route('/zhuce/',methods=['GET','POST'])
    def zhuce():
        if request.method =='GET':
            return  render_template('zhuce.html')
        else:
            usern = request.form.get('name')
            teln = request.form.get('password')
            passw = request.form.get('password1')
            user = User.query.filter(User.username==usern).first()
            if user:
                return 'username existed'
            else:
                user= User(username=usern,tel=teln,password=passw)
                db.session.add(user)
                db.session.commit()
                return redirect(url_for('login'))
    
    @app.route('/login/', methods=['GET','POST'])
    def login():
        if request.method == 'GET':
            return render_template('login.html')
        else:
            username = request.form.get('username')
            password = request.form.get('password')
            user = User.query.filter(User.username == username).first()
            if user:
                if user.password ==password:
                    session['user'] = username
                    return redirect(url_for('index'))
                else:
                    return u'password error'
            else:
                return u'username is not existed'
    
    
    @app.route('/question/',methods=['GET','POST'])
    def question():
        return render_template('question.html')
    
    if __name__ == '__main__':
        app.run(debug=True)
    

      

    import os
    SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@127.0.0.1:3306/login_db?charset=utf8'
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SECRET_KEY = os.urandom(24)
    

      

posted on 2017-11-23 20:40  黑木-007  阅读(117)  评论(0编辑  收藏  举报