Django開發(fā)中復(fù)選框用法示例
本文實(shí)例講述了Django開發(fā)中復(fù)選框用法。分享給大家供大家參考,具體如下:
一、查詢數(shù)據(jù)庫(kù)遍歷所有的復(fù)選框
1、python查詢數(shù)據(jù)庫(kù)所有的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ù)選框的值,是一個(gè)選中的數(shù)組
tags = request.POST.getlist('tags')
# 循環(huán)遍歷所有選中的復(fù)選框,利用多對(duì)多的關(guān)系追加到數(shù)據(jù)庫(kù)
for tag in tags:
blogModel.tag.add(tag)
return HttpResponseRedirect('book_add')
else:
return HttpResponse(u'是不被處理的請(qǐng)求方式')
2、前端頁(yè)面
<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、進(jìn)入編輯頁(yè)面,先獲取全部的復(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提交的時(shí)候注意要把復(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'驗(yàn)證錯(cuò)誤')
希望本文所述對(duì)大家Django框架的Python程序設(shè)計(jì)有所幫助。
- Python的Django框架中forms表單類的使用方法詳解
- 使用PyCharm配合部署Python的Django框架的配置紀(jì)實(shí)
- Django框架如何使用ajax的post方法
- 全面解讀Python Web開發(fā)框架Django
- 分析Python的Django框架的運(yùn)行方式及處理流程
- 詳解Python的Django框架中manage命令的使用與擴(kuò)展
- 詳解Python的Django框架中Manager方法的使用
- Python的Django框架中的表單處理示例
- 使用Python的Django框架實(shí)現(xiàn)事務(wù)交易管理的教程
- 在Python的Django框架中更新數(shù)據(jù)庫(kù)數(shù)據(jù)的方法
- Python的Django框架中的數(shù)據(jù)過濾功能
相關(guān)文章
python 實(shí)現(xiàn)全球IP歸屬地查詢工具
這篇文章主要介紹了python 實(shí)現(xiàn)全球IP歸屬地查詢工具的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運(yùn)行效率的原因,作者用代碼實(shí)踐了多個(gè)小片段來進(jìn)行對(duì)比解釋,需要的朋友可以參考下2015-04-04
python常用request庫(kù)與lxml庫(kù)操作方法整理總結(jié)
一路學(xué)習(xí),一路總結(jié),技術(shù)就是這樣,應(yīng)用之后,在進(jìn)行整理,才可以加深印象。本篇文字為小節(jié)篇,核心總結(jié) requests 庫(kù)與 lxml 庫(kù)常用的操作2021-08-08
Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子
這篇文章主要介紹了Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子,需要的朋友可以參考下2014-05-05
Python使用matplotlib創(chuàng)建Gif動(dòng)圖的思路
這篇文章主要介紹了Python使用matplotlib創(chuàng)建Gif動(dòng)圖,我們將討論matplotlib提供的名為“Animation”的動(dòng)畫庫(kù)之一,Python二維繪圖庫(kù)是Matplolib可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點(diǎn)圖等,需要的朋友可以參考下2022-04-04
python3 使用traceback定位異常實(shí)例
這篇文章主要介紹了python3 使用traceback定位異常實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python通過yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析
這篇文章主要介紹了Python通過yagmail實(shí)現(xiàn)發(fā)送郵件代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法
這篇文章主要介紹了Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
python繪制隨機(jī)網(wǎng)絡(luò)圖形示例
今天小編就為大家分享一篇python繪制隨機(jī)網(wǎng)絡(luò)圖形示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11

