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

Posted JaTae

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了完成登录功能,用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\'index.html\' %}
 {% block title %}
     登录
        {% endblock %}
{% block head %}
    <link href="{{ url_for(\'static\',filename=\'CSS/design.css\') }}" rel="stylesheet" type="text/css">
    <script src="{{ url_for(\'static\',filename=\'JS/confirm.js\') }}"></script>
{% endblock %}

{% block main %}
<div class="box" >
    <div class="jar" ><h2>欢迎登陆</h2></div>
    <form action="{{ url_for("login") }}" method="post">
        <div class="input_box">
            账号      <input id="name" name="username" type="text" placeholder="请输入用户名"></div><br>
        <div class="input_box">
            密码      <input id="password" name="password" type="password" placeholder="请输入密码"></div><br>

        <div id="error_box"><br></div>
         <div class="ja">
            <button onclick="return myLogin()" >登陆</button></div>
    </form></div>
{% endblock %}
function fnLogin() {
        var uname = document.getElementById("name")
        var uError = document.getElementById("error_box")
        var upassword = document.getElementById("password")
        var upasswordagain = document.getElementById("passwordagain")
        var isError =true;
        uError.innerHTML="<br>"
         if(uname.value.length<6 || uname.value.length >20){
        uError.innerHTML="用户名应为6到20字符";
        isError = false;
         return isError;
    }else if ((uname.value.charCodeAt(0)>=48) && (uname.value.charCodeAt(0)<=57)){
        uError.innerHTML="第一位只能是字母";
        isError = false;
         return isError;
    }
    if(upassword.value.length<6 || upassword.value.length>12){
        uError.innerHTML="密码应为6到20字符";
        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)

db = SQLAlchemy(app)
# 创建用户模型
class User(db.Model):
    __tablename__ = \'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)

# db.create_all()

@app.route(\'/\')
def homepage():
    return render_template("homepage.html")

@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(\'homepage\'))
            else:
                return \'password error\'
        else:
            return \'username is not existed\'


@app.route("/register/",methods=[\'GET\',\'POST\'])
def register():
    if request.method == "GET":
        return render_template("register.html")
    else:
        username = request.form.get("username")
        password = request.form.get("password")
        user = User.query.filter(User.username == username).first()
        if user:
            return \'username existed.\'
        else:
            user = User(username=username, password=password)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for(\'login\'))


@app.route("/question/")
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/mis_db?charset=utf8\'
SQLALCHEMY_TRACK_MODIFICATIONS = False

SECRET_KEY=os.urandom(24)

 

以上是关于完成登录功能,用session记住用户名的主要内容,如果未能解决你的问题,请参考以下文章

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

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

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

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

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

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