flask提供的request請(qǐng)求對(duì)象可以實(shí)現(xiàn)獲取url或表單中的字段值
GET請(qǐng)求
從URL中獲取name、age兩個(gè)參數(shù)
from flask import Flask,url_for,redirect,request
app=Flask(__name__)
@app.route('/')
def index():
name=request.args.get('name')
age=request.args.get('age')
message=f'姓名:{name}\n年齡:{age}'
return message
if __name__=='__main__':
app.run(
debug=True,
port=5000
)
POST請(qǐng)求
使用request.form可以接受表單數(shù)據(jù)
login.html模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用戶登錄</title>
</head>
<body>
<form action="" method="post">
<div>
<label for="username">用戶名</label>
<input type="text" id="username" name="username" value="">
</div>
<div>
<label for="password">密碼</label>
<input type="password" id="password" name="password" value="">
</div>
<button type="submit">提交</button>
</form>
</body>
</html>
獲取用戶名稱、密碼文章來源:http://www.zghlxwxcb.cn/news/detail-621947.html
from flask import Flask,url_for,redirect,request,render_template
app=Flask(__name__)
@app.route('/login',methods=['GET','POST'])
def login():
if request.method=='POST':
username=request.form['username']
password=request.form['password']
message=f'姓名:{username}</br>密碼:{password}'
return message
return render_template('login.html')
if __name__=='__main__':
app.run(
debug=True
)
文章來源地址http://www.zghlxwxcb.cn/news/detail-621947.html
到了這里,關(guān)于【Python】Web學(xué)習(xí)筆記_flask(2)——get&post的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!