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 %}
2014-06-25 22:33 GMT+02:00 Saloni Baweja <salonibaweja10@gmail.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'})
>
>
> --
> 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 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.
--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAL2Rd%3D%2BR1y1UNqy8h49whbV32OmqFTgc8%3Dgbpbk%3D6z-RH3eCHQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment