精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
flask 中get和post用法
get和post1、get請求:        使用場景:如果只對服務器獲取數據,并沒有對服務器產生任何影響,那么這時候使用get請求        傳參:get請求傳參是放在url中,并且是通過?的形式來指定key和value的2、post請求:        使用場景:如果要對服務器產生影響,那么使用post請求        傳參:post請求傳參不是放在URL中,是通過form data 的形式發送給服務器的get 其他年輕是通過flask.request.args來獲取post請求是通過flask.request.form來獲取post請求在模板中要注意幾點:*input 標簽中,要寫name來表示這個value的key,方便后臺獲取*在寫form表單的時候,要指定method=‘post’,并且要指定action='/login/'  
1、get實例:
模板文件index.html
<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>首頁</title></head><body><!--一旦點擊連接就會訪問視圖函數search,通過視圖函數來反轉URL,URL的參數是q,值為hello:就會返回url:http://127.0.0.1:5000/search/?q=hello-->    <a href="{{ url_for('search',q='hello') }}">跳轉到搜索頁面</a></body>

</html>

get_demo.py文件:

#encoding:utf-8from flask import Flask,render_template,requestapp = Flask(__name__)@app.route('/')def index(): #一訪問127.0.0.1:5000就會返回index模板中的鏈接”跳轉到搜索頁面”    return render_template('index.html')@app.route('/search/')def search():    #arguments    print request.args #獲取所有參數    print request.args.get('q') #或者參數為q的值    return 'search'@app.route('/login/',methods=['GET','POST'])def login():    if request.method == 'GET': #如果請求方法時GET,返回login.html模板頁面        return render_template('login.html')    else:        username = request.form.get('username')        password = request.form.get('password')if __name__ == '__main__':    app.run()
2、post實例


login.html模板文件:

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>首頁</title></head><body>    <form action="{{ url_for('login') }}" method="post">        <table>            <tbody>                <tr>                    <td>用戶名:</td>                    <td><input type="text" placeholder="請輸入用戶名" name="username"></td>                </tr>                <tr>                    <td>密碼:</td>                    <td><input type="text" placeholder="請輸入密碼" name="password"></td>                </tr>                <tr>                    <td></td>                    <td><input type="submit" value="登陸"></td>                </tr>            </tbody>        </table>    </form></body></html>
get_post_demo.py文件
#encoding:utf-8from flask import Flask,render_template,requestapp = Flask(__name__)@app.route('/')def index(): #一訪問127.0.0.1:5000就會返回index模板中的鏈接”跳轉到搜索頁面”    return render_template('index.html')@app.route('/search/')def search():    #arguments    print request.args #獲取所有參數    print request.args.get('q') #或者參數為q的值    return 'search'@app.route('/login/',methods=['GET','POST'])  #指定訪問頁面的方法def login():    if request.method == 'GET': #如果請求方法時GET,返回login.html模板頁面        return render_template('login.html')    else:        username = request.form.get('username')        password = request.form.get('password')        return 'post request'if __name__ == '__main__':    app.run()

本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
flask第十八篇——模板【2】
簡單而直接的Python web 框架:web.py
初識Flask
Flask 教程,第二部分:模板
python后臺Flask 快速入門
Flask是Python web最火熱三大框架之一!給我兩小時帶你完全入門
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 沙河市| 陆河县| 明星| 淳安县| 五河县| 宜兴市| 昔阳县| 广宁县| 太仆寺旗| 卢龙县| 韶山市| 新平| 保定市| 开封县| 改则县| 田阳县| 沽源县| 沂源县| 益阳市| 商洛市| 大石桥市| 灵川县| 乳源| 福清市| 巴南区| 周至县| 稷山县| 连南| 岐山县| 蚌埠市| 高邑县| 霍山县| 杭州市| 兴隆县| 舟曲县| 中西区| 肥乡县| 永德县| 普安县| 镇康县| 会昌县|