from flask import render_template, flash, redirect, url_for from app import app from app.forms import LoginForm from flask_login import current_user, login_user from app.models import User @app.route("/login", methods=["GET", "POST"]) def login(): if current_user.is_authenticated: return redirect(url_for("index")) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(username=form.username.data).first() if user is None or not user.check_password(form.password.data): flash("Invalid username or password") return redirect(url_for("login")) login_user(user) return redirect(url_for("index")) return render_template("login.html", title="Sign In", form=form) @app.route("/") @app.route("/index") def index(): user = {"username": "Miguel"} posts = [ {"author": {"username": "John"}, "body": "Beautiful day in Portland!"}, {"author": {"username": "Susan"}, "body": "The Avengers movie was so cool!"}, ] return render_template("index.html", title="Home", user=user, posts=posts)