from flask import Blueprint, render_template, session, flash, redirect, url_for

from app_02.forms import LoginForm
from app_02.models import User

blue = Blueprint('blue', __name__)


# 注册蓝图
def init_blue(app):
    app.register_blueprint(blueprint=blue)

#
# @blue.route('/')
# def hello_world():
#     # 创建模型对象
#
#     return 'Hello World!'


@blue.route('/login', methods=['POST','GET'])
def login():
    form = LoginForm()

    if form.validate_on_submit():

        username = form.name.data
        password = form.password.data

        if username == 'tom' and password == '123456':
            if username != session['username'] and password != session['password']:
                flash('账号或密码提交有鬼')
            session['username'] = form.name.data
            session['password'] = form.password.data
            print('信息是......',session.get('username'),session.get('password'))
            return redirect(url_for('blue.login'))
        else:
            msg = '账户或密码有误 '
            return render_template('form.html',msg=msg)



    return render_template('form.html',name=session.get('username'),
                           password=session.get('password'),
                           form=form)
