溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Django框架如何獲取form表單數(shù)據(jù)

發(fā)布時間:2020-08-01 10:32:38 來源:億速云 閱讀:324 作者:小豬 欄目:開發(fā)技術(shù)

這篇文章主要講解了Django框架如何獲取form表單數(shù)據(jù),內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。

Django中獲取text,password

  名字:<input type="text" name="name"><br><br>

  密碼:<input type="password" name="password">

  Form表單提交數(shù)據(jù)時使用的是post方式,所以在后端接收參數(shù)的時候需要先判斷請求方式為post時才能請求到數(shù)據(jù)

  name = request.POST.get('name')

  password = request.POST.get('password')

Django中獲取單選框

  性別:

                   <input type="radio" name="gender" value="man">男

    <input type="radio" name="gender" value="woman">女

    此時獲取到的值是woman或者man

    gender = request.POST.get('gender')

Django中獲取單選的復(fù)選框

  單選復(fù)選框:<input type="checkbox" name="is_tuanyuan" value="is_tuanyuan">是否是團員

  此時如果選中該選項,獲取到的值是value后面的,若沒有選中即是None

  is_tuanyuan = request.POST.get('is_tuanyuan')

Django中獲取復(fù)選框

  復(fù)選框:<input type="checkbox" name="joy" value="sing">唱歌

      <input type="checkbox" name="joy" value="dance">跳舞

  這里應(yīng)該使用getlist獲取多選框,獲取到的是列表形式,用get獲取只能得到最后一個選項

  joy = request.POST.getlist('joy')

Django中獲取單選下拉框

  去過哪些城市?單選

  <select name="city">

    <option>北京</option>

    <option>天津</option>

    <option>南京</option>

  </select>

  這里獲取到的就直接是option里面的內(nèi)容

  city = request.POST.get('city')

Django中獲取多選的下拉框

  去過哪些城市?多選

  <select multiple name="more_city">

    <option>北京</option>

    <option>天津</option>

    <option>南京</option>

  </select>

  這里涉及到多個值得獲取,需要使用getlist,獲取到的是列表,get依然只能獲取到一個值,用戶在使用時按住Ctrl即可以實現(xiàn)多選

  more_city = request.POST.getlist('more_city')

Django中獲取文本域

  <textarea name="more_text" placeholder="請輸入備注"></textarea>

  獲取方法:

  more_text = request.POST.get('more_text')

看完上述內(nèi)容,是不是對Django框架如何獲取form表單數(shù)據(jù)有進一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI