日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python Flask-表单提交方式

發(fā)布時(shí)間:2025/3/21 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python Flask-表单提交方式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這篇文章講兩種表單提交方式,先說一下目錄樹,下圖左側(cè)

templates文件夾放置html文件,
static文件夾放置css,js文件.

1.請(qǐng)求上下文

首先在templates文件夾新建一個(gè)login.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body> <div align="center"> <h2>User Management</h2>{% if message %} {{message}} {% endif %}<form method="POST"><input type="text" name="username" placeholder="username"><br><input type="password" name="password" placeholder="password"><br><input type="submit" value="Submit"><input type="reset" value="reset"></form> </div></body> </html>

{% if message %} {{message}} {% endif %}用于輸出登錄失敗時(shí)的錯(cuò)誤信息,在form標(biāo)簽中添加提交方式<form method="POST">

然后新建一個(gè)login.py

from flask import Flask,request,render_template,redirectapp = Flask(__name__) //綁定訪問地址127.0.0.1:5000/user @app.route("/user",methods=['GET','POST']) def login():if request.method =='POST':username = request.form['username']password = request.form['password']if username =="user" and password=="password":return redirect("http://www.baidu.com")else:message = "Failed Login"return render_template('login1.html',message=message)return render_template('login1.html')if __name__ == '__main__':app.run(debug=True)

2.WTForm

這里對(duì)上面login1.html和login.py修改

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title> </head> <body> <div align="center"> <h2>User Management</h2>{% if message %} {{message}} {% endif %}<form method="POST">username:{{form.username}}<br>password:{{form.password}}<br><input type="submit" value="Submit"><input type="reset" value="reset"></form> </div></body> </html>

這里直接去掉了輸入的input標(biāo)簽,換成了
username:{{form.username}},
password:{{form.password}} (jinja2模板)

from flask import Flask,request,render_template,redirectapp = Flask(__name__)from wtforms import Form,TextField,PasswordField,validatorsclass LoginForm(Form):username = TextField("username",[validators.Required()])password = PasswordField("password",[validators.Required()])@app.route("/user",methods=['GET','POST']) def login():myForm = LoginForm(request.form)if request.method =='POST':if myForm.username.data =="user" and myForm.password.data=="password" and myForm.validate():return redirect("http://www.baidu.com")else:message = "Failed Login"return render_template('login1.html',message=message,form=myForm)return render_template('login1.html',form=myForm)if __name__ == '__main__':app.run(debug=True)

(1)from wtforms import Form,TextField,PasswordField,validators
從wtforms 導(dǎo)入Form類,所有自定義的表單都需要繼承這個(gè)類,比如

class LoginForm(Form):

(2)一系列的Field對(duì)應(yīng)html的input標(biāo)簽控件,validators是驗(yàn)證器,用于驗(yàn)證用戶輸入的數(shù)據(jù).

myForm.password.data

以上代碼用于獲取用戶輸入的密碼,這里驗(yàn)證中多了myForm.validate():

(3)創(chuàng)建一個(gè)對(duì)象,[validators.Required()]表明這個(gè)值必須要輸入

username = TextField("username",[validators.Required()])

(4)在最后還需將myForm傳入form
return render_template('login1.html',form=myForm)

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Python Flask-表单提交方式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。