Django開發中核取方塊用法示例

2022-09-29 22:57:27 字數 1948 閱讀 7858

一、查詢資料庫遍歷所有的核取方塊

1、python查詢資料庫所有的tag

# 新增文章

def add(request):

if request.method == 'get':

tags = tagmodel.objects.all()

return render(request, 'books_add.html', )

elif request.method == 程式設計客棧'post':

title = request.post.get('title', none)

content = request.post.gproma**et('content', none)

blogmodel = blogmodel(title=title, content=content, author=authormodel.objects.get(id=1))

blogmodel.s**e()

# 獲取核取方塊的值,是乙個選中的陣列

tags = request.post.getlist('tags')

# 迴圈遍歷所有選中的核取方塊,利用多對多的關係追加到資料庫

return httpresponse(u'是不被處理的請求方式')

2、前端頁面標籤}

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', )

4、判斷如果選中的就勾選標籤

}}

二、ajax提交的時候注意要把核取方塊轉換字串提交

1、前端**

$('#btn').on('click', function 程式設計客棧(e)

})console.log(hobby);

$.ajax(,

'traditional': true,

'beforesend': function (xhr, settings) ,

'success': function (data)

})})

2、後端**

# 獲取核取方塊的值

hobby = request.post.getlist('hobby')

print '*' * 100

print hobby

print '*' * 100

return httpresponse(u'成功')

else:

return httpresponse(u'驗證錯誤')

本文標題: django開發中核取方塊用法示例

本文位址: /jiaoben/python/223184.html

MFC中核取方塊 checkbox

來自 http blog.csdn.net dongzhongshu archive 2010 06 30 5705138.aspx checkbox是一種特殊的按鈕 得到核取方塊狀態的函式 cbutton pbtn cbutton getdlgitem idc check int state pb...

QtableView中新增核取方塊

先建立乙個qt widgets 應用程式,並將mainwindow.cpp檔案中修改為如下 include mainwindow.h include ui mainwindow.h include include include include include include include inc...

php js 中多選項(複選)框的檢測

在php中,想獲得核取方塊的值,需要將核取方塊的name命名稱陣列的形式。然後在 get multiplayname 或 put multiplayname 獲得其數值,但是如何判斷使用者是否選中其中的標單了,多選專案必須選擇一項,否則不能提交表單!在網上找了半天的資料,基本上是對多選框的name處...