網(wǎng)站首頁 編程語言 正文
模板渲染
所謂模板渲染就是讓flask渲染一個(gè)html文檔,比如你有一個(gè)html文件,想要在網(wǎng)站上加載出來,你就要渲染它。
首先把這個(gè)文件,叫做模板渲染.html,放在templates
文件夾下面,
然后代碼中,導(dǎo)入render_template
類
from flask import Flask,render_template
另外也可以修改模板文件的渲染路徑,使用template_folder
來修改
app = Flask(__name__,template_folder='../fdf')
這里我們不修改。
模板渲染.html:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>模板渲染</title> </head> <body> <p>你好</p> <h3>How are you</h3> </body> </html>
模板渲染.py:
from flask import Flask,render_template app = Flask(__name__) @app.route('/') def index(): return render_template('模板渲染.html') if __name__ == '__main__': app.run()
運(yùn)行這個(gè)flask項(xiàng)目,在根路徑下/
,執(zhí)行對(duì)應(yīng)的視圖函數(shù),渲染對(duì)應(yīng)的html文件,顯示如下:
GET和POST請(qǐng)求
在設(shè)置路由的時(shí)候,可以設(shè)置訪問url的時(shí)候,接受的請(qǐng)求方式。
GET
請(qǐng)求表示瀏覽器需要get某一個(gè)文件,服務(wù)器就把這個(gè)url對(duì)應(yīng)的資源發(fā)給瀏覽器,默認(rèn)情況下,我們輸入url地址,就是在使用GET
請(qǐng)求的方式請(qǐng)求資源。比如:
@app.route('/',methods=["GET"]) def index(): return render_template('模板渲染.html')
methods=["GET"]
限定訪問方式,不寫,默認(rèn)就是GET方式。
表示在訪問根路徑/時(shí),只接受get
方式的請(qǐng)求,那我們輸入url,按回車就是get
方式,是可以訪問的。
如果修改成:@app.route('/',methods=["POST"])
當(dāng)我們點(diǎn)擊這個(gè)鏈接后:
就會(huì)發(fā)現(xiàn):
這就是因?yàn)槲覀兿拗屏烁窂?code>/的訪問只能用POST
,當(dāng)然也可以修改為GET,POST都可以的形式
@app.route('/',methods=["GET","POST"])
POST請(qǐng)求,表示的是我現(xiàn)在不是要獲取某個(gè)資源,而是我有數(shù)據(jù)需要提交給服務(wù)器,讓服務(wù)器來處理。
首先,我們的html
頁面為,這里有一個(gè)表單,需要我們填寫數(shù)據(jù),然后我們把數(shù)據(jù)交給服務(wù)器來處理,設(shè)置表單的action="http://localhost:5000/datahandle"
,也就意味著數(shù)據(jù)提交到/datahandle
這個(gè)頁面來處理
test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>post請(qǐng)求</title> </head> <body> <form action = "http://localhost:5000/datahandle" method = "post"> <table> <tr> <td>Name</td> <td><input type ="text" name ="username"></td> </tr> <tr> <td>Password</td> <td><input type ="password" name ="password"></td> </tr> <tr> <td><input type = "submit"></td> </tr> </table> </form> </form> </body> </html>
然后是py文件,
from flask import Flask,render_template,request app = Flask(__name__) @app.route('/') def index(): return render_template('test.html') #post數(shù)據(jù)到這個(gè)頁面來處理,直接輸入url是get方式訪問,訪問不了,這里限定了只能用POST方式訪問 @app.route('/datahandle',methods=["POST"]) def handle(): #獲取提交到的數(shù)據(jù) name = request.form['username'] pwd = request.form['password'] return f'name: {name}, password: {pwd}' if __name__ == '__main__': app.run()
進(jìn)入到根路徑后會(huì)渲染模板文件test.html
,然后在表單中輸入name,password后,提交給/datahandle
頁面來處理,它對(duì)應(yīng)的視圖函數(shù)handle()
處理數(shù)據(jù)。但是這里涉及到如何拿到表單中的數(shù)據(jù),需要使用request
類
from flask import Flask,render_template,request name = request.form['username'] pwd = request.form['password']
實(shí)際上,在form表單中輸入的數(shù)據(jù)都被flask以字典的形式存儲(chǔ)起來了,使用print(request.form)
可以查看
表單中輸入以下數(shù)據(jù):提交,
在控制臺(tái)中輸出ImmutableMultiDict([('username', '123'), ('password', '456')])
鍵值對(duì)username='123',password = '456'
然后使用request.form['username']
拿到key對(duì)應(yīng)的value,或者使用request.form.get('username')
一樣可以拿到數(shù)據(jù)。
提交后頁面跳轉(zhuǎn)到了/datahandle
中,處理完的數(shù)據(jù)返回出來:
成功的拿到了post方式提交的數(shù)據(jù)。
原文鏈接:https://blog.csdn.net/weixin_42576837/article/details/126166577
相關(guān)推薦
- 2022-11-01 淺談Android串口通訊SerialPort原理_Android
- 2022-08-01 C語言深入探索遞歸的特點(diǎn)_C 語言
- 2023-01-20 Python輸入圓半徑,計(jì)算圓周長和面積的實(shí)現(xiàn)方式_python
- 2022-09-19 golang圖片處理庫image基本操作_Golang
- 2023-01-30 react-router-dom?降低版本的兩種方法詳解_React
- 2022-10-07 Android開發(fā)Jetpack組件Lifecycle使用篇_Android
- 2022-05-13 數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記——順序存儲(chǔ)結(jié)構(gòu)實(shí)現(xiàn)棧
- 2022-03-05 Android中CountDownTimer類詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支