0

I am passing a list uans as a context for my template. I want to iterate over uans to display values within it. My code is as follows:

{% for q in qlist %}

    <span>You answered: {{ uans.{{forloop.counter0}} }}</span>

{% endfor %}

But I'm getting this error:

Could not parse the remainder: '{{forloop.counter0' from 'uans.{{forloop.counter0'

It works fine if I write uans.0 ,, uans.1 and so on. I want to use the forloop counter as an index. I want to iterate over uansonly using the counter.

Please suggest a solution. Thanks :)

5
  • Someone posted about filters but the comments have been closed.This is not a duplicate of the mentioned question @Sayse Commented Mar 23, 2016 at 16:21
  • I believe it is, you're trying to do the exact same thing I was doing. If it weren't a duplicate then what you're doing doesn't make sense, how do you know the users answer will always have the same index as the question? Commented Mar 23, 2016 at 16:23
  • My qlist has the questions and uans has the option which the user answered. qlist is a QuerySet. uans just has the options as strings inside it Commented Mar 23, 2016 at 16:25
  • I'm pretty sure it is a duplicate, you want something like questions_and_answers = zip(qlist, uans) in the view, then {% for q, a in questions_and_answers %} in your template. Commented Mar 23, 2016 at 16:38
  • I think i misunderstood the other question which @Sayse mentioned. I used a similar approach to that by using zip() and i'm getting the expected results. Thanks :) Commented Mar 23, 2016 at 16:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.