本文實(shí)例講述了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'是不被處理的請求方式')