日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Flask模板渲染與Get和Post請(qǐng)求詳細(xì)介紹_python

作者:weixin_42576837 ? 更新時(shí)間: 2022-11-01 編程語言

模板渲染

所謂模板渲染就是讓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

欄目分類
最近更新