建立用户认证系统需要经过以下几步:
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
...
db.init_app(app)
...
from . import db #导入声明的数据库
from werkzeug.security import generate_password_hash, check_password_hash #导入hash加密
#创建用户数据模型
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
@property
def password(self):
raise AttributeError('password is not a readable attribute')
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)
def __repr__(self):
return self.username
前两步用于建立数据库,用于第三步的表单验证
app/ auth/__ init__. py: 创建蓝本
from flask import Blueprint
auth = Blueprint
from . import views
app/auth/views.py:路由和视图函数
from flask import render_template
from . import auth
@auth.route('/')
def login():
return render_template('auth/login.html')
app/__ init__. py: 添加认证蓝本
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')
1.修改用户模型,支持用户登录
Flask-Login扩展要求程序的User模型需要具备以下几个方法:
可以利用flask-login的UserMixin类实现以上方法,即class User(UserMixin, db.Model):多重继承
2.app/auth/forms.py: 登录表单
from flask.ext.wtf import Form #导入表单
from wtforms import StringField, PasswordField, BooleanField, SubmitField #表单输入格式
from wtforms.validators import DataRequired, Length, Email #表单验证模块
class LoginForm(Form):
email = StringField('Email', validators=[DataRequired(), Length(1,64), Email()])
password = PasswordField('password', validators=[DataRequired()]) remember_me = BooleanField('Keep me Logged in ')
submit = SubmitField('Log In')
修改base.html,根据登录状态改变链接
{% if current_user.is_authenticated %}
SIGNOUT
{% else %}
SIGNIN
{% endif %}
判断条件中的变量 current_user 由 Flask-Login 定义,且在视图函数和模板中自动可用。这个变量的值是当前登录的用户,如果用户尚未登录,则是一个匿名用户。如果是匿名用户,is_authenticated() 方法返回 False。所以这个方法可用来判断当前用户是否已经登录。
3.用户登入登出
from flask import render_template, redirect, request, url_for, flash
from flask.ext.login import login_required, login_user, logout_user
from . import auth
from ..models import User
from .forms import LoginForm
@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data):
login_user(user, form.remember_me.data)
return redirect(request.args.get('next') or url_for('main.index'))
flash('Invalid username or password.')
return render_template('auth/login.html', form=form)
@auth.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out. ')
return redirect(url_for('main.index'))
{% extends 'base.html' %}
{% import 'bootstrap/wtf.html' as wtf %}
{% block title %}HOPTOP | Login {% endblock %}
{% block content %}
Login
{{ wtf.quick_form(form) }}
{% endblock %}
在数据中添加用户
db.create_all() # 如果没有创建数据库,执行这一步
u = User(email='[email protected]', username='admin', password='admin')
db.session.add(u)
db.session.commit() #提交
打开网页测试
中间会出现很多问题,细心的解决