Django開發(fā)中復選框用法示例
本文實例講述了Django開發(fā)中復選框用法。分享給大家供大家參考,具體如下:
一、查詢數(shù)據(jù)庫遍歷所有的復選框
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()
# 獲取復選框的值,是一個選中的數(shù)組
tags = request.POST.getlist('tags')
# 循環(huán)遍歷所有選中的復選框,利用多對多的關(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">標簽</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、進入編輯頁面,先獲取全部的復選框及選中的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">標簽</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提交的時候注意要把復選框轉(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當中設(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)
# 獲取復選框的值
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框架的配置紀實
- 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)文章
探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因,作者用代碼實踐了多個小片段來進行對比解釋,需要的朋友可以參考下2015-04-04
python常用request庫與lxml庫操作方法整理總結(jié)
一路學習,一路總結(jié),技術(shù)就是這樣,應(yīng)用之后,在進行整理,才可以加深印象。本篇文字為小節(jié)篇,核心總結(jié) requests 庫與 lxml 庫常用的操作2021-08-08
Python random模塊(獲取隨機數(shù))常用方法和使用例子
這篇文章主要介紹了Python random模塊(獲取隨機數(shù))常用方法和使用例子,需要的朋友可以參考下2014-05-05
Python使用matplotlib創(chuàng)建Gif動圖的思路
這篇文章主要介紹了Python使用matplotlib創(chuàng)建Gif動圖,我們將討論matplotlib提供的名為“Animation”的動畫庫之一,Python二維繪圖庫是Matplolib可以輕松創(chuàng)建繪圖、直方圖、條形圖、散點圖等,需要的朋友可以參考下2022-04-04
Python通過yagmail實現(xiàn)發(fā)送郵件代碼解析
這篇文章主要介紹了Python通過yagmail實現(xiàn)發(fā)送郵件代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法
這篇文章主要介紹了Numpy數(shù)組array和矩陣matrix轉(zhuǎn)換方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08

