-
Notifications
You must be signed in to change notification settings - Fork 0
/
6.message-flash.py
54 lines (43 loc) · 1.43 KB
/
6.message-flash.py
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
43
44
45
46
47
48
49
50
51
52
53
54
from flask import Flask, request, render_template, redirect, url_for, session, flash
from datetime import timedelta
app = Flask(__name__, template_folder="views")
# This is mandatory to work with strings, value can be anything
app.secret_key = "Hello"
# To save session for specific period
app.permanent_session_lifetime = timedelta(minutes=5)
@app.route("/")
def home():
return render_template("index.html", title="Home")
@app.route("/test")
def test():
return render_template("test.html")
@app.route("/login", methods=["POST", "GET"])
def login():
if request.method == "POST":
session.permanent = True
name = request.form["name"]
session["name"] = name
flash("You are logged in sucessfully !!")
return redirect(url_for("user"))
else:
if "name" in session:
flash("Already Loggedin !!!")
return redirect(url_for("user"))
return render_template("login.html")
@app.route("/user")
def user():
if "name" in session:
user = session['name']
return render_template("user.html", user=user)
else:
flash("you are not logged in")
return redirect(url_for("login"))
@app.route("/logout")
def logout():
if "name" in session:
user = session['name']
flash(f"you have been logged out {user}", "info")
session.pop("name", None)
return redirect(url_for("login"))
if __name__ == "__main__":
app.run(debug=True)