flask登录注册简单的例子

1.主程序

# app.py 

# Auther: hhh5460
# Time: 2018/10/05
# Address: DongGuan YueHua

from functools import wraps
from flask import Flask, request, render_template, redirect, url_for, flash, session

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import and_, or_

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.secret_key = '\xc9ixnRb\xe40\xd4\xa5\x7f\x03\xd0y6\x01\x1f\x96\xeao+\x8a\x9f\xe4'

db = SQLAlchemy(app)


############################################
# 数据库
############################################

# 定义ORM
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True)
    password = db.Column(db.String(80))
    email = db.Column(db.String(120), unique=True)

    def __repr__(self):
        return '<User %r>' % self.username
        

# 创建表格、插入数据
@app.before_first_request
def create_db():
    db.drop_all()  # 每次运行,先删除再创建
    db.create_all()
    
    admin = User(username='admin', password='root', email='admin@example.com')
    db.session.add(admin)

    guestes = [User(username='guest1', password='guest1', email='guest1@example.com'),
               User(username='guest2', password='guest2', email='guest2@example.com'),
               User(username='guest3', password='guest3', email='guest3@example.com'),
               User(username='guest4', password='guest4', email='guest4@example.com')]
    db.session.add_all(guestes)
    db.session.commit()
    

############################################
# 辅助函数、装饰器
############################################

# 登录检验(用户名、密码验证)
def valid_login(username, password):
    user = User.query.filter(and_(User.username == username, User.password == password)).first()
    if user:
        return True
    else:
        return False


# 注册检验(用户名、邮箱验证)
def valid_regist(username, email):
    user = User.query.filter(or_(User.username == username, User.email == email)).first()
    if user:
        return False
    else:
        return True

# 登录
def login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        # if g.user:
        if session.get('username'):
            return func(*args, **kwargs)
        else:
            return redirect(url_for('login', next=request.url)) # 
    return wrapper


############################################
# 路由
############################################

# 1.主页
@app.route('/')
def home():
    return render_template('home.html', username=session.get('username'))

# 2.登录
@app.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    if request.method == 'POST':
        if valid_login(request.form['username'], request.form['password']):
            flash("成功登录!")
            session['username'] = request.form.get('username')
            return redirect(url_for('home'))
        else:
            error = '错误的用户名或密码!'

    return render_template('login.html', error=error)

# 3.注销
@app.route('/logout')
def logout():
    session.pop('username', None)
    return redirect(url_for('home'))

# 4.注册
@app.route('/regist', methods=['GET','POST'])
def regist():
    error = None
    if request.method == 'POST':
        if request.form['password1'] != request.form['password2']:
            error = '两次密码不相同!'
        elif valid_regist(request.form['username'], request.form['email']):
            user = User(username=request.form['username'], password=request.form['password1'], email=request.form['email'])
            db.session.add(user)
            db.session.commit()
            
            flash("成功注册!")
            return redirect(url_for('login'))
        else:
            error = '该用户名或邮箱已被注册!'
    
    return render_template('regist.html', error=error)

# 5.个人中心
@app.route('/panel')
@login_required
def panel():
    username = session.get('username')
    user = User.query.filter(User.username == username).first()
    return render_template("panel.html", user=user)



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

2.主页

<!-- templates/home.html -->

<DOCTYPE html>
<html lang="zh-han">
    <head>
        <title>主页</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
        {% if username %}
            <p>{{username}},欢迎来到主页! <a href="{{ url_for('panel') }}">个人中心</a> <a href="{{ url_for('logout') }}">注销</a></p>
        {% else %}
            <p>欢迎来到主页! <a href="{{ url_for('login') }}">登录</a> <a href="{{ url_for('regist') }}">注册</a></p>
        {% endif %}
    </body>
</html>

3.登录页面

<!-- templates/login.html -->

<DOCTYPE html>
<html lang="zh-han">
<head>
    <title>登录</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
</head>
<body>
    <form method="post" action='/login'>
        <label>用户名:<input type="text" name="username" value=""></label><br>
        <label>密码:<input type="password" name="password" value=""></label><br>
        <input type="submit" value="登录">
    </form>
    
    {% if error %}
        <p>{{error}}</p>
    {% endif %}
</body>
</html>

4.注册页面

<!-- templates/regist.html -->

<DOCTYPE html>
<html lang="zh-han">
<head>
    <title>注册</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="css/style.css" rel="stylesheet">
</head>
<body>
    <form method="post" action='/regist'>
        <label>用户名:<input type="text" name="username" value=""></label></br>
        <label>密码:<input type="password" name="password1" value=""></label></br>
        <label>重复密码:<input type="password" name="password2" value=""></label></br>
        <label>邮箱:<input type="email" name="email" value=""></label></br>
        <input type="submit" value="注册">
    </form>
    
    {% if error %}
        <p>{{error}}</p>
    {% endif %}
</body>
</html>

5.个人中心页面

<!-- templates/panel.html -->

<DOCTYPE html>
<html lang="zh-han">
    <head>
        <title>个人中心</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href="css/style.css" rel="stylesheet">
    </head>
    <body>
        <h3>个人中心</h3>
        <p>姓名:{{user.username}}</p>
        <p>密码:{{user.password}}</p>
        <p>邮箱:{{user.email}}</p>
    </body>
</html>
posted @ 2018-10-05 20:42  罗兵  阅读(8895)  评论(0编辑  收藏  举报