Wednesday, June 25, 2014

Handling checkbox inputs

I'm building a small app that at first displays the form containing the checkboxes and asks the user to select one or more of them and then displays the name of only selected ones. 
This works fine. Now, I want to display the data only corresponding to the fields that the user has checked or selected. This creates problem as I'm not able to fetch the attributes or objects from the list object named ' info ' since it is list of unicode strings like [u'name', u'marks'] . In display.html {{ j.i }} is not being fetched as  ' i ' isn't an object but unicode string whereas ( when tried in shell ), j.marks fetches the information marks and name being objects or attributes. 
Templates :

 # form.html

<form action='/info/display' method="get">
<p><input type="checkbox" name="info" value="name"> Name </p>
<p><input type="checkbox" name="info" value="roll_no"> Roll No. </p>
<p><input type="checkbox" name="info" value="branch"> Branch </p>
<p><input type="checkbox" name="info" value="session"> Session </p>
<p><input type="checkbox" name="info" value="marks"> Marks </p>
<p><input type="checkbox" name="info" value="backlog"> Backlog </p>
<p> <input class="button" type="Submit" value="Submit"> </p>

# display.html

You selected for : {{ nothing }}
{% for i in info %}
{% for j in list %}
<p> {{ j.i }} </p>
{% endfor %}
{% endfor %}
{% if error %}
Please select atleast one field.
{% endif %}


def index(request):
return render(request, 'index.html')

def display(request):
list = Student.objects.all()
   if 'info' in request.GET:
       info = request.GET.getlist('info')
       return render(request, 'display.html', { 'info': info, 'list':list })
       return render(request, 'display.html', {'error': True, 'nothing':'nothing'})

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
To post to this group, send email to
Visit this group at
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment