flask - request 對(duì)象

flask 中的 request 對(duì)象用來獲取請(qǐng)求信息

其中有以下

  • args 所有 query 參數(shù)
  • form 所有 get 參數(shù)
  • cookies 所有 cookie 參數(shù)
from app import app
from flask import request, render_template_string, make_response


@app.route('/')
@app.route('/index')
def index():
    return '''Hello World. <br>
    click here to login: <a href="/login"> login </a> <br>
    click here to query: <a href="/query?a=1&b=2"> query </a> <br>
    click here to set cookie: <a href="/cookie"> set cookie </a>
    '''
  1. post 數(shù)據(jù)
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        if username == 'kur0mi':
            return 'login success.'
        else:
            return 'login failed.'
    
    return '''
    <form action="" method="post">
        username: <input type="text" name="username">
        <input type="submit" value="login">
    </form>
    '''
  1. get 數(shù)據(jù)
@app.route('/query')
def query():
    return render_template_string('''
    <table border=1>
        {% for k, v in args.items() %}
            <tr>
                <th> {{ k }} </th>
                <td> {{ v }} </td>
            </tr>
        {% endfor %}
    </table>
    ''', args=request.args)
  1. cookie 數(shù)據(jù)
@app.route('/cookie', methods=['GET', 'POST'])
def cookie():
    if request.method == 'POST':
        user = request.form['user']
        resp = make_response('''welcome to here to setcookie.
        ''')
        resp.set_cookie('user', user)
        return resp

    return render_template_string('''
    <form action="" method="post">
        user: <input type="text" name="user">
        <input type="submit" value="set cookie">
    </form>
    this are you cookies?
    <table border=1>
        {% for k, v in cookies.items() %}
            <tr>
                <th> {{ k }} </th>
                <td> {{ v }} </td>
            </tr>
        {% endfor %}
    </table>
    ''', cookies=request.cookies)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 22年12月更新:個(gè)人網(wǎng)站關(guān)停,如果仍舊對(duì)舊教程有興趣參考 Github 的markdown內(nèi)容[https://...
    tangyefei閱讀 35,439評(píng)論 22 257
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,715評(píng)論 19 139
  • 譯者說 Tornado 4.3于2015年11月6日發(fā)布,該版本正式支持Python3.5的async/await...
    TaoBeier閱讀 3,185評(píng)論 0 10
  • FLASK框架 簡介: flask是一個(gè)非常小的python web框架 只提供了一個(gè)強(qiáng)勁的核心 其它都...
    riverstation閱讀 2,404評(píng)論 2 38
  • 現(xiàn)在才終于看清迷茫的真正面目。 現(xiàn)在任職于一個(gè)教育機(jī)構(gòu)的小學(xué)教師,每天的工作內(nèi)容是備課,上課,作輔。一直工作將近有...
    ohana_bd57閱讀 290評(píng)論 0 0

友情鏈接更多精彩內(nèi)容