django - 对象的特定属性将随机显示在模板上
问题描述
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>
解决方案
推荐阅读
- arrays - 一半的维度如何给出NxN矩阵的层数?
- java - 制作 BigInteger 的副本
- sql-server - 如何在 S3 中查询异构 JSON 数据?
- java - Android - 在 AsyncTask 中定义“上下文”
- asp.net-mvc - 每次根据下拉列表更改表格内容时,如何从表格的第一行获取“名称”的值?
- c# - 更改枚举项在原始 IEnumerable 中未更改
- javascript - 我可以创建多个级别的嵌套对象吗?
- php - 从 php 脚本运行 php 文件
- angular - 如何删除使用 CLI 创建的 Angular 库(ng 生成库)
- c++ - 当算法需要派生类的知识时,将算法与数据解耦