django表單中的按鈕獲取數(shù)據(jù)的實例分析
以下是Django框架獲取各種form表單數(shù)據(jù)的方法
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="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里面的內(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')
知識點擴展:
Django:form表單和button獲取數(shù)據(jù)
如果想使用獲取數(shù)據(jù)
1.首先需要加上form表單:
<form> <button/> </form>
2.加上控件,比如select下拉框:
<form> <select name='selectname'></select><button/></form>
3.后端加上 if request.method==‘POST' (此處要大寫)就可以把你選擇的下拉框數(shù)據(jù)獲?。簐alue = request.POST.get(‘selectname')
到此這篇關(guān)于django表單中的按鈕獲取數(shù)據(jù)的實例分析的文章就介紹到這了,更多相關(guān)django表單中的按鈕怎么獲取數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Django表單外鍵選項初始化的問題及解決方法
- Django def clean()函數(shù)對表單中的數(shù)據(jù)進行驗證操作
- Django 構(gòu)建模板form表單的兩種方法
- Django form表單與請求的生命周期步驟詳解
- Django model.py表單設置默認值允許為空的操作
- Django表單提交后實現(xiàn)獲取相同name的不同value值
- Django框架獲取form表單數(shù)據(jù)方式總結(jié)
- django之從html頁面表單獲取輸入的數(shù)據(jù)實例
- 解決django中form表單設置action后無法回到原頁面的問題
- django-xadmin根據(jù)當前登錄用戶動態(tài)設置表單字段默認值方式
- Django給表單添加honeypot驗證增加安全性
相關(guān)文章
python將字母轉(zhuǎn)化為數(shù)字實例方法
在本篇文章里小編給大家整理的是關(guān)于python如何將字母轉(zhuǎn)化為數(shù)字的相關(guān)實例內(nèi)容,有需要的朋友們可以學習下。2019-10-10