**request模块:**request中封存了客户端的一些信息。可将request当全局变量使用。(P16)

指定视图函数接受的HTTP请求类型:@app.route('path', methods=[type1, type2, ...])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from flask import Flask, redirect, url_for, request
app = Flask(__name__)

@app.route('/success/<name>')
def success(name):
return 'welcome {}'.format(name)

@app.route('/login', methods = ['POST', 'GET'])
def login():
if request.method == 'POST':#用POST方法请求
user = request.form['nm']#从表单中提取数据
return redirect(url_for('success', name = user))
else:#用GET方法请求
user = request.args.get('nm')#args为字典,包含所有表单参数与其对应值
return redirect(url_for('success', name = user))

if __name__ == '__main__':
app.run(debug = True)