轻量Pythonweb - flask+jinja2
后台代码 MVC
from flask import Flask,request,render_template
app = Flask(__name__)
@app.route('/',methods=['GET','POST'])
def home():
return render_template('home.html')
@app.route('/signin',methods=['GET'])
def signin_form():
return render_template('form.html')
@app.route('/signin',methods=['POST'])
def sigin():
username = request.form['username']
password = request.form['password']
if username.strip() == 'admin' and password.strip() == 'password':
return render_template('signin-ok.html',username=username)
return render_template('form.html',message='Bad information...',username=username)
if __name__ == '__main__':
app.run()
View 都放到 templates 目录下
<html>
<head>
<title>Home</title>
</head>
<body>
<h1 style="font-style:italic">Home</h1>
</body>
</html>
<html>
<head>
<title>Please Sign In</title>
</head>
<body>
{% if message %}
<p style="color:red">{{ message }}</p>
{% endif %}
<form action="/signin" method="post">
<legend>Please sign in:</legend>
<p><input name="username" placeholder="Username" value="{{ username }}"></p>
<p><input name="password" placeholder="Password" type="password"></p>
<p><button type="submit">Sign In</button></p>
</form>
</body>
</html>
<html>
<head>
<title>Welcome, {{ username }}</title>
</head>
<body>
<p>Welcome, {{ username }}!</p>
</body>
</html>
如果有来生,一个人去远行,看不同的风景,感受生命的活力。。。