網站首頁 編程語言 正文
模板渲染
所謂模板渲染就是讓flask渲染一個html文檔,比如你有一個html文件,想要在網站上加載出來,你就要渲染它。
首先把這個文件,叫做模板渲染.html,放在templates
文件夾下面,
然后代碼中,導入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()
運行這個flask項目,在根路徑下/
,執行對應的視圖函數,渲染對應的html文件,顯示如下:
GET和POST請求
在設置路由的時候,可以設置訪問url的時候,接受的請求方式。
GET
請求表示瀏覽器需要get某一個文件,服務器就把這個url對應的資源發給瀏覽器,默認情況下,我們輸入url地址,就是在使用GET
請求的方式請求資源。比如:
@app.route('/',methods=["GET"]) def index(): return render_template('模板渲染.html')
methods=["GET"]
限定訪問方式,不寫,默認就是GET方式。
表示在訪問根路徑/時,只接受get
方式的請求,那我們輸入url,按回車就是get
方式,是可以訪問的。
如果修改成:@app.route('/',methods=["POST"])
當我們點擊這個鏈接后:
就會發現:
這就是因為我們限制了根路徑/
的訪問只能用POST
,當然也可以修改為GET,POST都可以的形式
@app.route('/',methods=["GET","POST"])
POST請求,表示的是我現在不是要獲取某個資源,而是我有數據需要提交給服務器,讓服務器來處理。
首先,我們的html
頁面為,這里有一個表單,需要我們填寫數據,然后我們把數據交給服務器來處理,設置表單的action="http://localhost:5000/datahandle"
,也就意味著數據提交到/datahandle
這個頁面來處理
test.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>post請求</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數據到這個頁面來處理,直接輸入url是get方式訪問,訪問不了,這里限定了只能用POST方式訪問 @app.route('/datahandle',methods=["POST"]) def handle(): #獲取提交到的數據 name = request.form['username'] pwd = request.form['password'] return f'name: {name}, password: {pwd}' if __name__ == '__main__': app.run()
進入到根路徑后會渲染模板文件test.html
,然后在表單中輸入name,password后,提交給/datahandle
頁面來處理,它對應的視圖函數handle()
處理數據。但是這里涉及到如何拿到表單中的數據,需要使用request
類
from flask import Flask,render_template,request name = request.form['username'] pwd = request.form['password']
實際上,在form表單中輸入的數據都被flask以字典的形式存儲起來了,使用print(request.form)
可以查看
表單中輸入以下數據:提交,
在控制臺中輸出ImmutableMultiDict([('username', '123'), ('password', '456')])
鍵值對username='123',password = '456'
然后使用request.form['username']
拿到key對應的value,或者使用request.form.get('username')
一樣可以拿到數據。
提交后頁面跳轉到了/datahandle
中,處理完的數據返回出來:
成功的拿到了post方式提交的數據。
原文鏈接:https://blog.csdn.net/weixin_42576837/article/details/126166577
相關推薦
- 2023-07-18 SpringBoot中無法用@Value獲取yml中的配置值的原因
- 2023-01-03 Android序列化實現接口Serializable與Parcelable詳解_Android
- 2022-06-27 Python利用re模塊實現簡易分詞(tokenization)_python
- 2022-11-11 ASP.NET?MVC實現下拉框多選_實用技巧
- 2022-06-08 FreeRTOS實時操作系統支持時間片示例詳解_操作系統
- 2022-06-17 Go模板template用法詳解_Golang
- 2022-04-03 Python中的tkinter庫簡單案例詳解_python
- 2022-07-14 python?numpy.ndarray中如何將數據轉為int型_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支