1. 程式人生 > >flask登錄註冊頁面

flask登錄註冊頁面

127.0.0.1 template ace alc filter nag data deb key

註冊登錄頁面index頁面

from flask import *
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.secret_key = ‘84375r0hfuggwkjvgfjhvsjhgv‘


# 數據庫連接
app.config[‘SQLALCHEMY_DATABASE_URI‘] = ‘mysql+pymysql://root:@127.0.0.1:3306/bookmanager‘
# 動態追蹤修改設置,如未設置只會提示警告
app.config[‘SQLALCHEMY_TRACK_MODIFICATIONS‘] = True
# 查詢時會顯示原始SQL語句
app.config[‘SQLALCHEMY_ECHO‘] = True
db = SQLAlchemy(app)


class Admin(db.Model):
__tablename__ = ‘admin‘
id = db.Column(db.Integer,primary_key=True)
user = db.Column(db.String(100), nullable=False)
pwd = db.Column(db.String(128), nullable=False)


@app.route(‘/init‘)
def init():
# db.create_all()
return ‘ok‘


@app.route(‘/admin/register‘, methods=[‘GET‘, ‘POST‘])
def index():
if request.method == ‘POST‘:
user = request.form.get(‘user‘)
pwd1 = request.form.get(‘pwd1‘)
pwd2 = request.form.get(‘pwd2‘)
if all([user, pwd1, pwd2]):
if pwd1 == pwd2:
a = Admin()
a.user = user
a.pwd = pwd1
db.session.add(a)
db.session.commit()
flash(‘註冊成功‘)
else:
flash(‘兩次密碼輸入不一致‘)
else:
flash(‘輸入信息不全‘)
return render_template(‘admin/register.html‘)
# request 從請求裏讀內容


@app.route(‘/admin/login‘, methods=[‘GET‘, ‘POST‘])
def login():
if request.method == ‘POST‘:
user = request.form.get(‘user‘)
pwd = request.form.get(‘pwd‘)
if all([user, pwd]):
a = Admin.query.filter(Admin.user == user).first()
if a:
# 如果用戶存在,判斷密碼是否正確
if a.pwd == pwd:
# 登錄成功後,session[‘admin_id‘]存入數據,
# 其他頁面用來判斷用戶到登錄狀態
session[‘admin_id‘] = a.id
flash(‘登陸成功‘)
# 登錄成功後跳轉到首頁,對圖書進行管理
return redirect(url_for(‘ind‘))
else:
flash(‘密碼錯誤‘)
else:
flash(‘用戶名不存在‘)
else:
flash(‘用戶名、密碼不完整‘)
return render_template(‘admin/login.html‘)


@app.route(‘/user‘)
def ind():
return ‘ok‘


if __name__ == ‘__main__‘:
app.run(debug=True)





2、HTML頁面
  (1)、login
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>
<form action="" method="post">
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
<br>
<label for="">用戶名</label><input type="text" name="user" placeholder="請輸入用戶名">
<br>
<label>密碼:</label><input type="password" name="pwd" placeholder="請輸入密碼">
<br>
<button type="submit">登錄</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登錄</title>
</head>
<body>
<form action="" method="post">
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
<br>
<label for="">用戶名</label><input type="text" name="user" placeholder="請輸入用戶名">
<br>
<label>密碼:</label><input type="password" name="pwd" placeholder="請輸入密碼">
<br>
<button type="submit">登錄</button>
</form>
</body>
</html>


  (2)register
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理員註冊</title>
</head>
<body>
<form action="" method="post">
{% for message in get_flashed_messages() %}
{{ message }}
{% endfor %}
<br>
<label for="">用戶名</label><input type="text" name="user" placeholder="請輸入用戶名">
<br>
<label>密碼:</label><input type="password" name="pwd1" placeholder="請輸入密碼">
<br>
<label>確認密碼:</label><input type="password" name="pwd2" placeholder="請確認密碼">
<br>
<button type="submit">註冊</button>
</form>
</body>
</html>

flask登錄註冊頁面