dimanche 14 août 2016

Django models and views clarification needed

I am just starting to learn Django. I am trying to create a Quiz Application. These are my models.

class Question(models.Model):
    quest=models.CharField(max_length=1000)

    def __unicode__(self):
        return self.quest

class Options(models.Model):
    question=models.ForeignKey(Question,on_delete=models.CASCADE)
    option1=models.CharField(max_length=1000)
    option2=models.CharField(max_length=1000)
    option3=models.CharField(max_length=1000)
    option4=models.CharField(max_length=1000)
    answer=models.IntegerField()

    def __unicode__(self):
        return u'%s %s %s %s %s' % (self.question, self.option1,self.option2,self.option3,
                                    self.option4)

class Selected_option(models.Model):
    question=models.ForeignKey(Question,on_delete=models.CASCADE)
    selected_option=models.IntegerField(blank=True, null=True)

    def __unicode__(self):
        return u'%s %s' %(self.question,self.selected_option)

This a view i have written.

def quizzing(request):
    questions=Question.objects.all()
    return render(request, 'quiz/land.html',{'questions':questions})

Here is a basic template i have written

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Quiz</title>
</head>
<body>
<h1> My Quiz </h1>

</body>
</html>

I want to display a question and its 4 options in my html page. What should I do in my views and templates?




Aucun commentaire:

Enregistrer un commentaire