黑松山资源网 Design By www.paidiu.com
本文实例讲述了Django开发中复选框用法。分享给大家供大家参考,具体如下:
一、查询数据库遍历所有的复选框
1、python查询数据库所有的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() # 获取复选框的值,是一个选中的数组 tags = request.POST.getlist('tags') # 循环遍历所有选中的复选框,利用多对多的关系追加到数据库 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() # 利用正向查找关于本博客选择的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提交的时候注意要把复选框转换字符串提交
1、前端代码
$('#btn').on('click', function (e) { // 设置空数组 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当中设置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程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月08日
2024年10月08日
- 金铲铲之战S9阵容推荐攻略 6套阵容分享
- dnf手游追忆光环外观选哪个 dnf手游追忆光环外观选择推荐
- 怎么获得dnf手游紫色曲玉 dnf手游紫色曲玉获取方法一览
- dnf手游洗点水怎么获得 dnf手游洗点水获取方法介绍
- 万代南梦宫遭中国玩家抵制 《铁拳8》被疯狂刷差评
- 曝侃爷分手登微博热搜 网友:穿衣意见不合?
- 《暗喻幻想:ReFantazio》M站2024高分榜排名第三 超越《最终幻想7:重生》
- 赛季末发育路上分英雄推荐,狄仁杰A就行
- 小乔克制攻略:除了兰陵王还有更多好选
- 王者荣耀:对抗路难度排行,花木兰最能秀
- 群星《新说唱2024 第3期 (下)》[FLAC/分轨][89.04MB]
- 群星《歌手2024 第2期》[320K/MP3][95.59MB]
- 群星《歌手2024 第2期》[FLAC/分轨][260.73MB]
- 《原神》5.0基尼奇一图流养成攻略 基尼奇怎么培养
- 5.1版本up池角色一览,新版本角色卡池