Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
comparing value from inputfield with django model data in js function

I have already made an app that allows me to study. It contains multiple test with multiple question with one correct answer each.


class Test(models.Model):
    name = models.CharField(max_length=200)
    #questions = models.ManyToManyField(Question)
    author = models.ForeignKey(User, on_delete=models.CASCADE, default=None, null=True, blank=True)
    date_posted = models.DateTimeField(auto_now_add = True)

    def get_questions(self):
        return self.question_set.all()

    def __str__(self):
        return self.name

class Question(models.Model):

    text = models.CharField(max_length=200, null=True)
    test = models.ForeignKey(Test, on_delete=models.CASCADE)
    created = models.DateTimeField(auto_now_add = True)

    def get_answer(self):
        return self.answer_set.all()

    def __str__(self):
        return self.text

class Answer(models.Model):
    text = models.CharField(max_length=200)
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name='parent')
    def __str__(self):
         return self.text

I made JS funtion that compare user answer from input field with correct answer put in html

function PointsCounter(){
   var points = 0;
   const elements = document.querySelectorAll('.question-container');
   Array.from(elements).forEach((element, index) => {
      var CorrectAnswer = (((element.querySelector('div form p')).innerHTML).toLowerCase()).trim();
    
    var UserAnswer = (((element.querySelector('div form p input')).value).toLowerCase()).trim();

   if(UserAnswer == CorrectAnswer){
   points++;
   }})
    document.getElementById("points").innerHTML=points;

                    
    }

Instead of putting correct answer in my html code:

<p id="ans" hidden>{{ question.parent.all.last }}</p>

I'd like to compare user answer with answer from database, but I do not know how to achieve it.

I mean, i'd like to pass correct answer to js function to disallow user to check answer by inspecting html code of page. How can I make it ?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda