How do I print the selected checkboxes names in django

20 views
Skip to first unread message

santosh reddy

unread,
Dec 28, 2015, 4:14:00 AM12/28/15
to Django users
Hi All,

Please find the reuirement:

1) Need to retrieve the test cases from database
2) provide an option to user to selcect the test cases which he want to run.
3) Need to send the selected test cases to target.

forms.py
class CustomCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
    def __init__(self, attrs=None):
        super(CustomCheckboxSelectMultiple, self).__init__(attrs)

    def render(self, name, value, attrs=None, choices=()):
        output = super(CustomCheckboxSelectMultiple, self).render(name, value, attrs, choices)

        style = self.attrs.get('style', None)
        if style:
            output = output.replace("<ul", format_html('<ul style="{0}"', style))

        return mark_safe(output)
class TestSuitesForm(forms.ModelForm):
      TestSuitess = forms.ModelMultipleChoiceField(queryset=TestSuites.objects.all(),widget=CustomCheckboxSelectMultiple(attrs={'style': 'list-style: none; margin: 0;'}),required=False, label="TestSuitess",show_hidden_initial=False)  
      class Meta:
             model = TestSuites
             fields = ['TestSuitess']

views.py:
from django.http import HttpResponse
from .models import TestSuites
from django.forms import ModelForm
from django import forms
from django.shortcuts import render_to_response
from mtf.forms import *
from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.contrib.auth import logout
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponseRedirect
from .models import TestSuites

def index(request):
    form = TestSuitesForm(request.POST)
    variables = RequestContext(request, {
    'form': form
    })
    return render_to_response(
    'home.html',
    variables,
    )
    
    #return HttpResponse(form)

def logout_page(request):
      /*Here i need to print the user selected chekboxes names.*/
models.py:
from __future__ import unicode_literals

from django.db import models

# Create your models here.
class TestSuites(models.Model):
      test_suites = models.CharField(max_length=30)
      component=models.BooleanField()
      def __str__(self):
         return self.test_suites

Thanks - Santosh

  
Reply all
Reply to author
Forward
0 new messages