Skip to content

Commit 4b631fc

Browse files
committed
add Flask-9-flash
1 parent fe7e782 commit 4b631fc

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

Flask-9-flash/run.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from flask import Flask, render_template, request, redirect, url_for, flash
2+
3+
app = Flask(__name__)
4+
app.secret_key = "xxx"
5+
6+
7+
@app.route('/')
8+
def index():
9+
return render_template('index.html')
10+
11+
12+
@app.route('/login', methods=['GET', 'POST'])
13+
def login():
14+
error = None
15+
if request.method == "POST":
16+
if request.form['email'] != 'test@gmail.com' or request.form['password'] != 'test':
17+
error = "Invalid account."
18+
else:
19+
flash("Login successfully")
20+
return redirect(url_for('index'))
21+
22+
return render_template('login.html', error=error)
23+
24+
25+
if __name__ == '__main__':
26+
app.run(debug=True)

Flask-9-flash/templates/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Index</title>
6+
</head>
7+
<body>
8+
{% with messages = get_flashed_messages() %}
9+
{% if messages %}
10+
{% for message in messages %}
11+
<p>{{ message }}</p>
12+
{% endfor %}
13+
{% endif %}
14+
{% endwith %}
15+
16+
<h3>Welcome!</h3>
17+
<a href = "{{ url_for('login') }}">login</a>
18+
</body>
19+
</html>

Flask-9-flash/templates/login.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Login</title>
6+
</head>
7+
<body>
8+
<form method = "post" action = "http://localhost:5000/login">
9+
<table>
10+
<tr>
11+
<td>Email</td>
12+
<td><input type = 'email' name = 'email'></td>
13+
</tr>
14+
<tr>
15+
<td>Password</td>
16+
<td><input type = 'password' name = 'password'></td>
17+
</tr>
18+
<tr>
19+
<td><input type = "submit" value = "Submit"></td>
20+
</tr>
21+
</table>
22+
</form>
23+
24+
{% if error %}
25+
<p><strong>Error</strong>: {{ error }}</p>
26+
{% endif %}
27+
</body>
28+
</html>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
5. [GET和POST](https://xugaoxiang.com/2020/03/18/flask-5-get-post/)
77
6. [Cookie和Session](https://xugaoxiang.com/2020/03/19/flask-6-cookie-session/)
88
7. [文件上传](https://xugaoxiang.com/2020/03/20/flask-7-file-upload/)
9-
8. [重定向](https://xugaoxiang.com/2020/03/23/flask-8-redirect/)
9+
8. [重定向](https://xugaoxiang.com/2020/03/23/flask-8-redirect/)
10+
9. [闪现消息](https://xugaoxiang.com/2020/03/25/flask-9-flash/)

0 commit comments

Comments
 (0)