Handling checkbox inputs

75 views
Skip to first unread message

Saloni Baweja

unread,
Jun 25, 2014, 4:33:24 PM6/25/14
to django...@googlegroups.com
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.name, 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>
</form>

# 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 %}


# views.py

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 })
   else:
       
       return render(request, 'display.html', {'error': True, 'nothing':'nothing'})
  

Roman Klesel

unread,
Jun 26, 2014, 3:33:47 AM6/26/14
to django...@googlegroups.com
Hello,

the {{j.i}} can not work as you expect. If i understand you correctly
you would like to do something like getattr(list, i) in the template.

In order to do this you have 2 possibilities:

1) Write a custom template tag like this
http://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template

2) Prepare the data in the view so that you can iterate over it more easily:

result = []
for i in info:
for j in list:
result.append(getattr(j, i))
return render(request, 'display.html', {'result': result})

{% for r in result %}
<p>{{r}}</p>
{% endfor %}
> --
> 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...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/523ff8cd-14e7-48d7-8c4f-28e5d0f05c3b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Saloni Baweja

unread,
Jun 26, 2014, 2:29:31 PM6/26/14
to django...@googlegroups.com


On Thursday, June 26, 2014 1:03:47 PM UTC+5:30, roman wrote:
Hello,

the {{j.i}} can not work as you expect. If i understand you correctly
you would like to do something like getattr(list, i) in the template.

In order to do this you have 2 possibilities:

1) Write a custom template tag like this
http://stackoverflow.com/questions/844746/performing-a-getattr-style-lookup-in-a-django-template

2) Prepare the data in the view so that you can iterate over it more easily:

result  = []
for i in info:
  for j in list:
    result.append(getattr(j, i))
return render(request, 'display.html', {'result': result})

{% for r in result %}
 <p>{{r}}</p>
{% endfor %}



While waiting for the reply, I worked out another method to do this. If interested you may have a look at https://github.com/saloni10/Django_SchoolApp/tree/master/info/template and https://github.com/saloni10/Django_SchoolApp/blob/master/info/views.py
I tried this too and it worked. Thanks ! 
Reply all
Reply to author
Forward
0 new messages