您好,登錄后才能下訂單哦!
這篇文章主要講解了django表單中的按鈕獲取數(shù)據(jù)的方法,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
Django中獲取text,password
名字:<input type="text" name="name"><br><br> 密碼:<input type="password" name="password"> Form表單提交數(shù)據(jù)時(shí)使用的是post方式,所以在后端接收參數(shù)的時(shí)候需要先判斷請求方式為post時(shí)才能請求到數(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">女 此時(shí)獲取到的值是woman或者man gender = request.POST.get('gender')
Django中獲取單選的復(fù)選框
復(fù)選框:<input type="checkbox" name="joy" value="sing">唱歌 <input type="checkbox" name="joy" value="dance">跳舞 這里應(yīng)該使用getlist獲取多選框,獲取到的是列表形式,用get獲取只能得到最后一個(gè)選項(xiàng) 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> 這里涉及到多個(gè)值得獲取,需要使用getlist,獲取到的是列表,get依然只能獲取到一個(gè)值,用戶在使用時(shí)按住Ctrl即可以 實(shí)現(xiàn)多選 more_city = request.POST.getlist('more_city')
Django中獲取文本域
<textarea name="more_text" placeholder="請輸入備注"></textarea> 獲取方法: more_text = request.POST.get('more_text')
知識點(diǎn)擴(kuò)展:
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ù)獲取:value = request.POST.get(‘selectname')
看完上述內(nèi)容,是不是對django表單中的按鈕獲取數(shù)據(jù)的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。