Numbered loop in Jinja

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I'm trying to make a quiz app using Django, Jinja2 and PostgreSQL. I want to number the questions but I don't know how to because python doesn't use a counter in for loops (e.g. for exercise in exercises - no for loop here)

I've tried to change the for loop

(for exercise in exercises)

so it has a counter

(for x in myrange)

and defining myrange in views.py to be a range(1, len(exercitii+1)) but after that I didn't know how to access the elements inside the Jinja template. I tried to access the exercises with

{exercitii.x-1.intrebare}}

but it didn't work.

html template


{% extends 'base.html' %}
{% load static %}
{% block content %}
    div>
    <div id="middle-section" class="container-fluid container-fluid-margin">
            <div class="content-quiz">
                <form action="{% url 'exercitiu' lectii.id %}" method="POST" id="quiz">
                        {% for exercitiu in exercitii %}
                        <div class="row content-block"> 
                                <div class="col-lg-10 text-card">
                                        {% csrf_token %}
                                            <div class="card card-custom">
                                                <div class="card-body">
                                                    <h5 class="card-title">{{exercitiu.id}} - {{exercitiu.intrebare}}h5>
                                                    <div class="card-text">
                                                            <input type="radio" name="question-{{exercitiu.id}}-answers" id="question-{{exercitiu.id}}-answers-A" value="A" />
                                                            <label for="question-{{exercitiu.id}}-answers-A">A) {{exercitiu.variante.0}} label>
                                                    div>
                                                    <div class="card-text">
                                                                <input type="radio" name="question-{{exercitiu.id}}-answers" id="question-{{exercitiu.id}}-answers-B" value="B" />
                                                                <label for="question-{{exercitiu.id}}-answers-B">B) {{exercitiu.variante.1}} label>
                                                    div> 
                                                    <div class="card-text">
                                                                <input type="radio" name="question-{{exercitiu.id}}-answers" id="question-{{exercitiu.id}}-answers-C" value="C" />
                                                                <
                                                
                                                
0 views
0 shares
profilepic.png
manpreet 2 years ago

If I got your question correctly, you want to get the iteration number of the for loop in jinja.

Jinja provides a very convenient loop variable, it has a property called loop.counter which is 1-indexed, and loop.counter0 which is 0-indexed. View Docs

And if you're using DjangoTemplate, then those become forloop.counter0 and forloop.counter. View Docs


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.