Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda