Monday, December 28, 2015

How do I print the selected checkboxes names in django

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

  

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e1db87de-a02b-48b5-a056-cfd47497d347%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment