flask页面的get与post请求
get请求
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎来到京东购物网站</h1>
<div>
<ul>
<li>hello</li>
<li>abc</li>
<li>world</li>
</ul>
<form action="/register1" method="get">
<p><input type="text" name='username' placeholder="请输入用户名"></p>
<p><input type="text" name='address' placeholder="请输入地址"></p>
<P><input type="submit" value="提交"></P>
</form>
</div>
</body>
</html>
flask解析get请求
from flask import Flask import setting app = Flask(__name__) app.config.from_object(setting) @app.route('/register') def register(): return render_template('index.html') @app.route('/register1') def register1(): #如果请求是get,需要用request.args来取值 print(request.args.get('username')) print(request.args.get('address')) return '进来了' if __name__ == '__main__': print(app.url_map) app.run(host='127.0.0.1', port=5000)
结果
post请求
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>首页</title> </head> <body> <h1>欢迎来到京东购物网站</h1> <div> <ul> <li>hello</li> <li>abc</li> <li>world</li> </ul> <form action="/register1" method="post"> <p><input type="text" name='username' placeholder="请输入用户名"></p> <p><input type="text" name='address' placeholder="请输入地址"></p> <P><input type="submit" value="提交"></P> </form> </div> </body> </html>
flask解析post请求
from flask import Flask import setting app = Flask(__name__) app.config.from_object(setting) @app.route('/register') def register(): return render_template('index.html')
@app.route('/register1',methods =['get','post'])
def register1():
#如果请求是post,则需要用request.form取值
print(request.form.get('username'))
print(request.form.get('address'))
return '进来了'
if __name__ == '__main__':
print(app.url_map)
app.run(host='127.0.0.1', port=5000)
结果: