from flask import render_template, make_response, request, redirect, url_for, flash from app import app import hashlib """ # 临时处理插入一下数据 from sqlalchemy.orm import sessionmaker from app import db """ # 用户权限认证相关数据 from app.login_form import UserForm from app.login_model import User from flask_login import LoginManager,login_user,login_required,logout_user # use login manager to manage session login_manager = LoginManager(app) login_manager.login_view = 'login' # 设置登录页面 # 回调函数,用来加载用户 @login_manager.user_loader # def load_user(id): '''用于加载用户''' return User.query.get(int(id)) """ 首页相关路由配置 """ @app.route('/') @app.route('/index') def index(): item = { "msg": "后台传递信息" } nav = dict() nav['h'] = 'active' # 访问首页生成一个 Cookie 值,该值用于访问特定页面 rendered_template = render_template('index.html', title='梦想橡皮擦', nav=nav) resp = make_response(rendered_template) text = "梦想橡皮擦" # 使用 sha256 算法进行加密 encrypted_text = hashlib.sha256(text.encode()).hexdigest() resp.set_cookie('story', encrypted_text) return resp @app.route('/bt') def bt(): return render_template('bt.html') @app.route('/mac') def mac(): return render_template('_form.html') """博客搭建清单""" @app.route('/blog') def blog(): nav = dict() nav['b'] = 'active' return render_template('blog.html', nav=nav) # 建站时间轴 @app.route('/timeline') def timeline(): nav = dict() nav['time'] = 'active' return render_template('timeline.html', nav=nav) @app.route("/login", methods=['GET', 'POST']) def login(): form = UserForm() print(form.validate_on_submit()) """ # 创建session类型 DBSession = sessionmaker(bind=db.engine) # 创建session对象 session = DBSession() # 创建新的user对象 new_user = User() new_user.username = 'xiangpica' new_user.password = '123456' session.add(new_user) # 提交即保存到数据库 session.commit() # 关闭session session.close() """ if form.validate_on_submit(): # 查询用户信息 user = User.query.filter_by(username=form.username.data).first() if user is not None: if user.verify_password(form.password.data): flash('登录成功') login_user(user) # login_user 的参数为要登录的用户 return redirect(request.args.get('next') or url_for('school.login_list_school')) flash('登录失败') return render_template('login.html', form=form) # 退出登录 @app.route('/logout') @login_required def logout(): logout_user() return redirect(url_for('login'))