0
I have tried to show an specific attribute values randomly. Just  dictionaries are appeared  in my  web page which I have showed. I want to show the actual questions which are stored in my Question model
        

class Exam(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE,related_name='exam_user')
    subject=models.ForeignKey(Subject,on_delete=models.CASCADE,related_name='exam_subject',default=True)
    choices={
        (1,'QUIZ'),
        (2,'MIDTERM'),
        (3,'FINAL')
    }
    exam_type=models.IntegerField(choices=choices)
    exam_mark=models.IntegerField()
    question=models.ManyToManyField(Question,related_name='exam_question')
    exam_date=models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering=('-exam_date',)


    def __str__(self):
        return 'Exam-type'+'-'+str(self.exam_type)
``````````````````````````````````````````````````````````````````````````````

class Question(models.Model):
    subject = models.OneToOneField(Subject,on_delete=models.CASCADE, related_name='question_subject')
    question_title=models.CharField(max_length=5000,unique=True)
    option_first=models.CharField(max_length=100,default='')
    option_second=models.CharField(max_length=100,default='')
    option_third= models.CharField(max_length=100,default='')
    option_fourth= models.CharField(max_length=100,default='')
    mark=models.IntegerField()

    def __str__(self):
        return (self.question_title)+'--1--'+str(self.option_first)+'--2--'+str(self.option_second)+'--3--'+str(self.option_third)+'--4--'+str(self.option_fourth)


这是我创建查询并将其传递给我的模板的视图

def showExam(request,id): ​exam_list = Exam.objects.filter(id=id) ​questionlist=Exam.objects.filter(id=id).order_by('?').values('question').distinct () ​dict={'exam':exam_list,'questionlist':questionlist} ​return render(request,'question_app/showExam.html',context=dict)

    <table class="table table-bordered table-striped ">
        <tr>
            <th>Exam Type</th>
            <th>User Id</th>
            <th>Question List</th>
            <th>Exam Mark</th>
            <th>Exam Date</th>
        </tr>
        {% for row in exam %}
        <tr>
            <td>{{ row.exam_type }}</td>
            <td>{{ row.id }}</td>
            <td>
                {% for qes in questionlist %}
                <ol>{{ qes }}</ol>
                {% endfor %}
            </td>
            <td>{{ row.exam_mark }}</td>
            <td>{{ row.exam_date }}</td>
        </tr>
        {% endfor %}
    </table>

图片中有一些随机出现的问题,但无法从中获取本词典中的真实问题

4

0 回答 0