Django開發(fā)中復(fù)選框用法示例
更新時間:2018年03月20日 12:04:26 作者:水痕01
這篇文章主要介紹了Django開發(fā)中復(fù)選框用法,結(jié)合實例形式分析了Django基于ajax的復(fù)選框遍歷、提交及后臺數(shù)據(jù)庫查詢等相關(guān)操作技巧,需要的朋友可以參考下
本文實例講述了Django開發(fā)中復(fù)選框用法。分享給大家供大家參考,具體如下:
一、查詢數(shù)據(jù)庫遍歷所有的復(fù)選框
1、python查詢數(shù)據(jù)庫所有的tag
# 新增文章 def add(request): if request.method == 'GET': tags = TagModel.objects.all() return render(request, 'books_add.html', {'tags': tags}) elif request.method == 'POST': title = request.POST.get('title', None) content = request.POST.get('content', None) blogModel = BlogModel(title=title, content=content, author=AuthorModel.objects.get(id=1)) blogModel.save() # 獲取復(fù)選框的值,是一個選中的數(shù)組 tags = request.POST.getlist('tags') # 循環(huán)遍歷所有選中的復(fù)選框,利用多對多的關(guān)系追加到數(shù)據(jù)庫 for tag in tags: blogModel.tag.add(tag) return HttpResponseRedirect('book_add') else: return HttpResponse(u'是不被處理的請求方式')
2、前端頁面
<div class="form-group"> <label class="col-sm-2 control-label">標(biāo)簽</label> <div class="col-sm-9"> {% for tag in tags %} <label class="checkbox-inline"> <input value="{{ tag.id }}" type="checkbox" name="tags"/>{{ tag.name }} </label> {% endfor %} </div> </div>
3、進入編輯頁面,先獲取全部的復(fù)選框及選中的id
# 編輯博客 def edit(request, blog_id): tags = TagModel.objects.all() # 利用正向查找關(guān)于本博客選擇的tag blogModel = BlogModel.objects.filter(id=blog_id).first() # 獲取全部的tag check_tag = blogModel.tag.all() # 獲取選中的id check_id = [int(x.id) for x in check_tag] print check_id return render(request, 'books_edit.html', {'tags': tags, 'check_id': check_id})
4、判斷如果選中的就勾選
<div class="form-group"> <label class="col-sm-2 control-label">標(biāo)簽</label> <div class="col-sm-9"> {% for tag in tags %} {% if tag.id in check_id %} <label class="checkbox-inline"> <input value="{{ tag.id }}" type="checkbox" name="tags" checked="checked"/>{{ tag.name }} </label> {% else %} <label class="checkbox-inline"> <input value="{{ tag.id }}" type="checkbox" name="tags"/>{{ tag.name }} </label> {% endif %} {% endfor %} </div> </div>
二、ajax提交的時候注意要把復(fù)選框轉(zhuǎn)換字符串提交
1、前端代碼
$('#btn').on('click', function (e) { // 設(shè)置空數(shù)組 var hobby = []; $('#hobby-group').find('input[type=checkbox]').each(function () { if ($(this).prop("checked")) { var hobbyId = $(this).val(); hobby.push(hobbyId); } }) console.log(hobby); $.ajax({ 'url': '/ajaxpost/', 'method': 'post', 'data': { 'username': $('.username').val(), 'hobby': hobby }, 'traditional': true, 'beforeSend': function (xhr, settings) { var csrftoken = ajaxpost.getCookie('csrftoken'); //2.在header當(dāng)中設(shè)置csrf_token的值 xhr.setRequestHeader('X-CSRFToken', csrftoken); }, 'success': function (data) { console.log(data); } }) })
2、后端代碼
@require_http_methods(['POST']) def ajaxpost(request): form = LoginForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username', None) # 獲取復(fù)選框的值 hobby = request.POST.getlist('hobby') print '*' * 100 print hobby print '*' * 100 return HttpResponse(u'成功') else: return HttpResponse(u'驗證錯誤')
希望本文所述對大家Django框架的Python程序設(shè)計有所幫助。
您可能感興趣的文章:
- Python的Django框架中forms表單類的使用方法詳解
- 使用PyCharm配合部署Python的Django框架的配置紀(jì)實
- Django框架如何使用ajax的post方法
- 全面解讀Python Web開發(fā)框架Django
- 分析Python的Django框架的運行方式及處理流程
- 詳解Python的Django框架中manage命令的使用與擴展
- 詳解Python的Django框架中Manager方法的使用
- Python的Django框架中的表單處理示例
- 使用Python的Django框架實現(xiàn)事務(wù)交易管理的教程
- 在Python的Django框架中更新數(shù)據(jù)庫數(shù)據(jù)的方法
- Python的Django框架中的數(shù)據(jù)過濾功能
相關(guān)文章
python numpy中multiply與*及matul 的區(qū)別說明
這篇文章主要介紹了python numpy中multiply與*及matul 的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05python3 unicode列表轉(zhuǎn)換為中文的實例
今天小編就為大家分享一篇python3 unicode列表轉(zhuǎn)換為中文的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10