Flask框架學習筆記之使用Flask實現(xiàn)表單開發(fā)詳解
本文實例講述了使用Flask實現(xiàn)表單開發(fā)。分享給大家供大家參考,具體如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div align="center"> <h1>User Management</h1> <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>
使用html實現(xiàn)的表單:
用flask實現(xiàn)相同功能的表單:
# -*- coding:utf-8 -*- from flask import Flask, request, render_template, redirect from wtforms import Form, TextField, PasswordField, validators app = Flask(__name__) class LoginForm(Form): # validators指定一個由驗證函數(shù)組成的列表 # 在接受用戶提交的數(shù)據(jù)之前驗證數(shù)據(jù) # 驗證函數(shù)Required()確保提交的字段不為空 username = TextField("username", [validators.Required()]) password = PasswordField("password", [validators.Required()]) # 定義user路由 @app.route("/user", methods=['GET', 'POST']) def login(): myForm = LoginForm(request.form) if request.method == 'POST': # username = request.form['username']使用request獲取數(shù)據(jù) # password = request.form['password'] # 也可以使用類實例里的表單方法來獲取相應(yīng)的數(shù)據(jù) # validate來驗證輸入的表單數(shù)據(jù)是否有效 if myForm.username.data == "loli" and myForm.password.data == "520" and myForm.validate(): return redirect("http://www.baidu.com") else: message = "Login Failed" return render_template("form1.html", message=message, form=myForm) return render_template("form1.html", form=myForm) if __name__ == '__main__': app.run()
form1模板:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div align="center"> <h1>User Management</h1> <form method="post"> {% if message %} {{ message }} {% endif %} <br> {{ form.username }} <br> {{ form.password }} <br> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> </div> </body> </html>
一樣的效果圖。
在WTForm3.0中Textfield被移除,使用Stringfield代替。
WTForm主要在flask中用于驗證表單。
參考官方文檔:http://dormousehole.readthedocs.io/en/latest/patterns/wtforms.html
希望本文所述對大家基于flask框架的Python程序設(shè)計有所幫助。
相關(guān)文章
一文教會你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對比數(shù)據(jù),折線圖主要用于展示數(shù)據(jù)變化的趨勢,散點圖主要用于判斷數(shù)據(jù)的相關(guān)性,下面這篇文章主要給大家介紹了關(guān)于如何通過一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關(guān)資料,需要的朋友可以參考下2022-11-11pycharm的debug調(diào)試以及異常,Python中錯誤的處理過程
這篇文章主要介紹了pycharm的debug調(diào)試以及異常,Python中錯誤的處理過程,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼
這篇文章主要介紹了Python3 ID3決策樹判斷申請貸款是否成功的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05Python Pytorch深度學習之神經(jīng)網(wǎng)絡(luò)
今天小編就為大家分享一篇關(guān)于Pytorch神經(jīng)網(wǎng)絡(luò)的文章,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10python GUI庫圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細使用方法
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5瀏覽器控件QWebEngineView詳細使用方法,需要的朋友可以參考下2020-02-02Python實現(xiàn)的遠程文件自動打包并下載功能示例
這篇文章主要介紹了Python實現(xiàn)的遠程文件自動打包并下載功能,結(jié)合實例形式分析了Python使用spawn()方法執(zhí)行ssh、scp 命令實現(xiàn)遠程文件的相關(guān)操作技巧,需要的朋友可以參考下2019-07-07