0

I have an app for some quiz with questions and choices. So I'm trying to render all this stuff to Django templates. In my views.py it looks like this

def choice(request):
    question_list = get_list_or_404(Question)
    page = get_object_or_404(Page, name='about')
    letters = ["A", "B", "C", "D", "E"]
    return render(request,
                  'qview/choice.html',
                  {
                    'question_list': question_list,
                    'page': page,
                    'letters': letters,
                   }
                  )

I have a list of questions and list with letters. All of that I'm sending as context to my template.

  {% if question_list %}
    <ul>
    {% for question in question_list %}
      <li><a href="#">{{question.question}}</a></li>
        <ul>
          {% for choice in question.choice_set.all %}
          <li>{{ choice.text }}</li>
          {% endfor %}
        </ul>
    {% endfor %}
    </ul>
  {% else %}
    <p>No questions available</p>
  {% endif %} 

So here I'm going through all of questions and all of choices connected to this question. But I can't get how I can also go through letters list? I was thinking about zip it all. But in view I have only questions not choices, so I can't zip to it.

So what else is possible here?

  • Could you explain where/how you would like to go through the `letters` list? As in, should that be a nested loop (possible) or in a loop subsequent to the first (also possible)? – Oerd Jan 11 '23 at 10:28
  • Yeah, letters should be in the second nested loop, on this line -
  • {{ choice.text }}
  • So it'll be like A choice01, B choice 02 and so on – Iggy Comdore Jan 11 '23 at 10:31