<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.htmlYou 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.pydef 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'})
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 %}