routes.py 787 Bytes
from flask import render_template, request, redirect, url_for, flash
from flask_login import login_user
from app.models import User

from . import bp


@bp.route('/login')
def login():
    return render_template('login.html', title="Login")


@bp.route('/login', methods=['POST'])
def login_post():
    user_login = request.form.get('login')
    user_password = request.form.get('password')
    user_remember = request.form.get('remember')
    user = User.query.filter_by(login=user_login).one_or_none()
    if user and user.password == user_password:
        # login code goes here
        login_user(user)
        flash("Succesfull login")
        return redirect(url_for('main.index'))
    else:
        flash("Wrong login or password")
        return redirect(url_for('auth.login'))