Skip to content

Commit b001d0c

Browse files
committed
add flask app with jinja2 templates
1 parent c54bfc1 commit b001d0c

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

py3/web/mvc/app.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from flask import Flask, request, render_template
5+
6+
app = Flask(__name__)
7+
8+
@app.route('/', methods=['GET', 'POST'])
9+
def home():
10+
return render_template('home.html')
11+
12+
@app.route('/signin', methods=['GET'])
13+
def signin_form():
14+
return render_template('form.html')
15+
16+
@app.route('/signin', methods=['POST'])
17+
def signin():
18+
username = request.form['username']
19+
password = request.form['password']
20+
if username=='admin' and password=='password':
21+
return render_template('signin-ok.html', username=username)
22+
return render_template('form.html', message='Bad username or password', username=username)
23+
24+
if __name__ == '__main__':
25+
app.run()

py3/web/mvc/templates/form.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<html>
2+
<head>
3+
<title>Please Sign In</title>
4+
</head>
5+
<body>
6+
{% if message %}
7+
<p style="color:red">{{ message }}</p>
8+
{% endif %}
9+
<form action="/signin" method="post">
10+
<legend>Please sign in:</legend>
11+
<p><input name="username" placeholder="Username" value="{{ username }}"></p>
12+
<p><input name="password" placeholder="Password" type="password"></p>
13+
<p><button type="submit">Sign In</button></p>
14+
</form>
15+
</body>
16+
</html>

py3/web/mvc/templates/home.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Home</title>
4+
</head>
5+
<body>
6+
<h1 style="font-style:italic">Home</h1>
7+
</body>
8+
</html>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<head>
3+
<title>Welcome, {{ username }}</title>
4+
</head>
5+
<body>
6+
<p>Welcome, {{ username }}!</p>
7+
</body>
8+
</html>

0 commit comments

Comments
 (0)