溫馨提示×

溫馨提示×

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

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

django獲取單數(shù)據(jù)的方法

發(fā)布時間:2020-08-07 11:24:05 來源:億速云 閱讀:130 作者:小新 欄目:編程語言

這篇文章主要介紹django獲取單數(shù)據(jù)的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

本文是總結Django獲取不同表單的一些方法,希望對大家有幫助。

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中獲取單選的復選框

單選復選框:<input type="checkbox" name="is_tuanyuan" value="is_tuanyuan">是否是團員
  此時如果選中該選項,獲取到的值是value后面的,若沒有選中即是None
  is_tuanyuan = request.POST.get('is_tuanyuan')

Django中獲取復選框

復選框:<input type="checkbox" name="joy" value="sing">唱歌
      <input type="checkbox" name="joy" value="dance">跳舞
  這里應該使用getlist獲取多選框,獲取到的是列表形式,用get獲取只能得到最后一個選項
  joy = request.POST.getlist('joy')

Django中獲取單選下拉框 

去過哪些城市?單選
  <select name="city">
    <option>北京</option>
    <option>天津</option>
    <option>南京</option>
  </select>
  這里獲取到的就直接是option里面的內容
  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')

以上是django獲取單數(shù)據(jù)的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

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

AI