main.py
1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from flask import Blueprint, render_template, flash, request, redirect, url_for
from flask_login import login_user, logout_user, login_required
from flaskr.extensions import cache
from flaskr.forms import LoginForm
from flaskr.models import User
main = Blueprint('main', __name__)
@main.route('/')
@cache.cached(timeout=1000)
def home():
return render_template('index.html')
@main.route("/login", methods=["GET", "POST"])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data).one()
login_user(user)
flash("Logged in successfully.", "success")
return redirect(request.args.get("next") or url_for(".home"))
return render_template("login.html", form=form)
@main.route("/logout")
def logout():
logout_user()
flash("You have been logged out.", "success")
return redirect(url_for(".home"))
@main.route("/restricted")
@login_required
def restricted():
return "You can only see this if you are logged in!", 200