This article example describes checkbox usage in Django development. Shared for your reference, as follows:
I. Querying the database to traverse all checkboxes
1, python query database all tag
# New articles added def add(request): if == 'GET': tags = () return render(request, 'books_add.html', {'tags': tags}) elif == 'POST': title = ('title', None) content = ('content', None) blogModel = BlogModel(title=title, content=content, author=(id=1)) () # Get the value of the checkbox, which is an array of checked boxes. tags = ('tags') # Loop over all checked checkboxes and append to the database using many-to-many relationships. for tag in tags: (tag) return HttpResponseRedirect('book_add') else: return HttpResponse(u'is the way the request is not processed')
2、Front-end page
<div class="form-group"> <label class="col-sm-2 control-label">tab (of a window) (computing)</label> <div class="col-sm-9"> {% for tag in tags %} <label class="checkbox-inline"> <input value="{{ }}" type="checkbox" name="tags"/>{{ }} </label> {% endfor %} </div> </div>
3, enter the edit page, first get all the check boxes and selected ids
# Edit the blog def edit(request, blog_id): tags = () # Use forward lookup to find tags about this blog's choices blogModel = (id=blog_id).first() # Get all the tags check_tag = () # Get the selected id check_id = [int() for x in check_tag] print check_id return render(request, 'books_edit.html', {'tags': tags, 'check_id': check_id})
4, judgment if the selected check box
<div class="form-group"> <label class="col-sm-2 control-label">tab (of a window) (computing)</label> <div class="col-sm-9"> {% for tag in tags %} {% if in check_id %} <label class="checkbox-inline"> <input value="{{ }}" type="checkbox" name="tags" checked="checked"/>{{ }} </label> {% else %} <label class="checkbox-inline"> <input value="{{ }}" type="checkbox" name="tags"/>{{ }} </label> {% endif %} {% endfor %} </div> </div>
Second, ajax submit the attention to the checkbox to convert the string submitted
1、Front-end code
$('#btn').on('click', function (e) { // Setting up empty arrays var hobby = []; $('#hobby-group').find('input[type=checkbox]').each(function () { if ($(this).prop("checked")) { var hobbyId = $(this).val(); (hobbyId); } }) (hobby); $.ajax({ 'url': '/ajaxpost/', 'method': 'post', 'data': { 'username': $('.username').val(), 'hobby': hobby }, 'traditional': true, 'beforeSend': function (xhr, settings) { var csrftoken = ('csrftoken'); //2. Set the value of csrf_token in the header. ('X-CSRFToken', csrftoken); }, 'success': function (data) { (data); } }) })
2. Back-end code
@require_http_methods(['POST']) def ajaxpost(request): form = LoginForm() if form.is_valid(): username = form.cleaned_data.get('username', None) # Get the value of the checkbox hobby = ('hobby') print '*' * 100 print hobby print '*' * 100 return HttpResponse(u'Success') else: return HttpResponse(u'Validation Error')
I hope that the description of this article will help you in Python Programming for Django Framework.