赞
踩
from flask import Flask
from flask import request
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return '<h1>Home</h1>'
@app.route('/signin', methods=['GET'])
def signin_form():
return '''<form action="/signin" method="post">
<p><input name="username"></p>
<p><input name="password" type="password"></p>
<p><button type="submit">Sign In</button></p>
</form>'''
@app.route('/signin', methods=['POST'])
def signin():
# 需要从request对象读取表单内容:
if request.form['username']=='admin' and request.form['password']=='password':
return '<h3>Hello, admin!</h3>'
return '<h3>Bad username or password.</h3>'
if __name__ == '__main__':
app.run()
==========================================
from flask import Flask,request,render_template
#from jinja2 import Environment, PackageLoader
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
return '<h1>hello</h1>'
@app.route('/signin', methods=['GET'])
def signin_form():
return render_template('form.html')
@app.route('/signin', methods=['POST'])
def signin():
# 需要从request对象读取表单内容:
if request.form['username']=='admin' and request.form['password']=='password':
print(render_template('home.html',username = request.form['username']))
return render_template('home.html',username = request.form['username'])
return (render_template('form.html',message = 'Bad username or password'))
if __name__ == '__main__':
app.debug = True
app.run()
<html>
<head>
Please Sign in
</head>
<body>
{%if message %}
<p style="color:red">{{ message }}</p>
{%endif%}
<form action="/signin" method="post">
<p><input name="username"></p>
<p><input name="password" type="password"></p>
<p><button type="submit">Sign In</button></p>
</form>
</body>
</html>
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome, {{ username }}</title>
</head>
<body>
<img src = ./static/test.PNG>
<p>Welcome, {{ username }}!</p>
</body>
</html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。