登录功能完成:
- js:设置return
- html:设置
- form
- input
-
οnclick="return fnLogin()"
- py:
- @app.route设置methods
- GET
- POST
- 读取表单数据
- 查询数据库
- 用户名密码对:
- 记住用户名
- 跳转到首页
- 用户名密码不对:
- 提示相应错误。
- 用户名密码对:
session:
- 从`flask`中导入`session`
- 设置`SECRET_KEY`
- 操作字典一样操作`session`:增加用户名`session['username']=`username
index.py
import config from flask import Flask, render_template, url_for, redirect, request,session from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object(config) 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) nickname = db.Column(db.String(20)) db.create_all() @app.route('/') def shouye(): return render_template('text.html') @app.route('/zhuce',methods=['GET','POST']) def zhuce(): if request.method == 'GET': return render_template('zhuce.html') else: username = request.form.get('username') password = request.form.get('password') nickname = request.form.get('nickname') user1 = User.query.filter(User.username == username).first() if user1: return 'username existed' else: user1 = User(username=username, password=password, nickname=nickname) db.session.add(user1) db.session.commit() return redirect(url_for('denglu')) @app.route('/denglu',methods=['GET','POST']) def denglu(): if request.method == 'GET': return render_template('denglu.html') else: username = request.form.get('username') password = request.form.get('password') user2=User.query.filter(User.username == username).first() if user2: if user2.password == password: return redirect(url_for('shouye')) else: return 'password error' else: return 'username is not existed' @app.route('/neirong') def fankui(): return render_template('fankui.html') if __name__ == '__main__': app.run(debug='True')
denglu.html
<!DOCTYPE html> <html lang="en"> <head> {% extends "text.html" %} <meta charset="UTF-8"> <title>登陆</title> <link href="https://cdn.bootcss.com/bootstrap/4.0.0-beta/css/bootstrap.css" rel="stylesheet"> <link href="https://cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"> {% block js %} <script src="{{ url_for('static',filename='js/Mylogin.js') }}"></script>{% endblock %} </head> <body background="../../AAA/WeChat%20Image_20171027161942.png" style="background-repeat:no-repeat;background-size: 100% 100%; background-attachment: fixed;"> {% block denglu %} <form id="b" method="post" action="{{ url_for('denglu') }}"> <div class="container-fluid " id="denglu"> <div class="mt-5 pt-5" > <div class="card border-primary mb-3 m-auto" style="max-width: 20rem;"> <div class="card-header text-center">wgd</div> <div class="card-body text-primary"> <h4 class="card-title">用户登录</h4> <div class="input-group mb-2 mr-sm-2 mb-sm-0"> <div class="input-group-addon"><i class="fa fa-user"></i></div> <input type="text" class="form-control" placeholder="Username" id="uname" required="true" name="username"> </div> <p id="errorbox1" class="m-0 p-0 text-danger">  </p> <div class="input-group mb-2 mr-sm-2 mb-sm-0"> <div class="input-group-addon"><i class="fa fa-lock"></i></div> <input type="password" class="form-control" placeholder="Password" id="upass" name="password"> </div> <p id="errorbox2" class="m-0 p-0 text-danger">  </p> <div class="row"> <div class="col-3"> <button class="btn btn-outline-primary" onclick=" MyLogin()" >登录</button> </div> <div class="col-9 text-sm-right"> <a href="{{ url_for('zhuce') }}" >注册 </a>|<a href="#" class="text-sm-right"> 忘记密码?</a> </div> </div> </div> </div> </div> </div></form>{% endblock %} </body> </html>
截图