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 '' % self.username
        

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

    guestes = [User(username='guest1', password='guest1', email='[email protected]'),
               User(username='guest2', password='guest2', email='[email protected]'),
               User(username='guest3', password='guest3', email='[email protected]'),
               User(username='guest4', password='guest4', email='[email protected]')]
    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.主页





    
        主页
        
        
        
    
    
        {% if username %}
            

{{username}},欢迎来到主页! 个人中心 注销

{% else %}

欢迎来到主页! 登录 注册

{% endif %}

3.登录页面






    登录
    
    
    


    


{% if error %}

{{error}}

{% endif %}

4.注册页面






    注册
    
    
    


    




{% if error %}

{{error}}

{% endif %}

5.个人中心页面





    
        个人中心
        
        
        
    
    
        

个人中心

姓名:{{user.username}}

密码:{{user.password}}

邮箱:{{user.email}}

转载于:https://www.cnblogs.com/hhh5460/p/9745812.html

你可能感兴趣的:(python,数据库)