0
I’m doing a quiz in Django and using Javascript as well. However, I want each question to create a dictionary containing the question, options and the correct option. I was doing it that way:
const myQuestions = [
{
question: "{{ quiz.0.1 }}",
answers: {
"a": "{{ quiz.0.2 }}",
"b": "{{ quiz.0.3 }}",
"c": "{{ quiz.0.4 }}",
"d": "{{ quiz.0.5 }}"
},
correctAnswer: "{{ quiz.0.6 }}"
},
{
question: "{{ quiz.1.1 }}",
answers: {
"a": "{{ quiz.1.2 }}",
"b": "{{ quiz.1.3 }}",
"c": "{{ quiz.1.4 }}",
"d": "{{ quiz.1.5 }}"
},
correctAnswer: "{{ quiz.1.6 }}"
}
Only that way it would be manual, and wanted, using a for to make it more automatic. But, I’m having trouble trying to do:
const myQuestions = [
{% for item in quiz %}
{
question: "{{ item.question }}",
answers: {
"a": "{{ item.option1 }}",
"b": "{{ item.option2 }}",
"c": "{{ item.option3 }}",
"d": "{{ item.option4 }}"
},
correctAnswer: "{{ item.answer }}"
}
{% endfor %}
];
This is the part that takes the questions, options and answer:
myQuestions.forEach(
(currentQuestion, questionNumber) => {
// variable to store the list of possible answers
const answers = [];
// and for each available answer...
for(letter in currentQuestion.answers){
// ...add an HTML radio button
answers.push(
`<label class="text-sans-serif text-dark radio">
<input type="radio" name="question${questionNumber}" value="${letter}" required>
<span>${currentQuestion.answers[letter]}</span>
</label>`
);
}
// add this question and its answers to the output
output.push(
`<div class="slide">
<div class="question mt-0 text-center"> ${currentQuestion.question} </div>
<div class="answers"> ${answers.join("")} </div>
</div>`
);
}
);
```
Simplesmente ele não aparece nada.
If you want, I’ll send you all the code.
– user185986
Hi, it’s always 6?
– Sergio
It’s the question, the four options and the answer.
– user185986
But there are many questions, so I would like the FOR
– user185986
I think you’re just missing the comma at the end of the object before
{% endfor %}
. If it is not that you can put here the result of that is when it arrives to the browser?– Sergio
It returns nothing. Stays blank.
– user185986
[Result Print] (https://i.imgur.com/0FJBuiJ.png) OBS: These buttons were not meant to appear, only the "Next Question".
– user185986
Can you show HTML (Javascript in the case of this loop) instead of this image? and you have an error in the console?
– Sergio
No error appears. The entire Javascript code is here: https://ghostbin.co/paste/rwasb
– user185986
In this link are still parts of Django...
{% for item in quiz %}
. This will render in a certain file?– Sergio
py
@login_required
def conhecimento_geral(request):
 return render(request, "quiz1.html", {"quiz": Quizzes.objects.get_queryset().all()})

– user185986
I wanted to take the values and create dicts to each question automatically. Javascript will do a foreach and get each value, understand? Only the problem is in Django’s for.
– user185986
I realize, from the beginning... this Django has a loop to generate certain Javascript objects?
– Sergio
Django (Quiz) returns all questions, options and answers. The loop would be to create dictionaries with each question.
– user185986
The problem is there
– user185986
I managed to solve, thank you very much anyway.
– user185986