Creating a Quiz in Django and Javascript

Asked

Viewed 92 times

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.

  • Hi, it’s always 6?

  • It’s the question, the four options and the answer.

  • But there are many questions, so I would like the FOR

  • 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?

  • It returns nothing. Stays blank.

  • [Result Print] (https://i.imgur.com/0FJBuiJ.png) OBS: These buttons were not meant to appear, only the "Next Question".

  • Can you show HTML (Javascript in the case of this loop) instead of this image? and you have an error in the console?

  • No error appears. The entire Javascript code is here: https://ghostbin.co/paste/rwasb

  • In this link are still parts of Django... {% for item in quiz %}. This will render in a certain file?

  • py&#xA;@login_required&#xA;def conhecimento_geral(request):&#xA; return render(request, "quiz1.html", {"quiz": Quizzes.objects.get_queryset().all()})&#xA;

  • 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.

  • I realize, from the beginning... this Django has a loop to generate certain Javascript objects?

  • Django (Quiz) returns all questions, options and answers. The loop would be to create dictionaries with each question.

  • The problem is there

  • I managed to solve, thank you very much anyway.

Show 11 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.