我需要使用随机问题在Django上制作测验应用,下面给出了Question模型。我已经想出了如何将查询集中的问题随机化,但是现在我需要找到一种方法来随机整理模板中问题的选项,但是我自己无法弄清楚。感谢您的任何建议
class Question(models.Model):
question = models.CharField('Question text',max_length=250)
test = models.ForeignKey(Test, on_delete=models.CASCADE)
Option1 = models.CharField('answer 1',max_length=50)
Option2 = models.CharField('answer 2',max_length=50)
Option3 = models.CharField('answer 3',max_length=50)
Option4 = models.CharField('answer 4',max_length=50)
RightAnsw = models.IntegerField('Right answer nuber')
mark = models.IntegerField('Points for right answer')
template.html
<form method="post">
{% csrf_token %}
{% for question in questions %}
<p class="questiontext">Question {{ forloop.counter }}/{{ questions.count }}: {{ question.question }}</p>
<p class="questionmark">Points:{{ question.mark }}</p>
<p class="label">Choices:</p>
<p class="option"><input type="radio" name="answer-{{ question.id }}" value="1">{{ question.Option1 }}</p>
<p class="option"><input type="radio" name="answer-{{ question.id }}" value="2">{{ question.Option2 }}</p>
<p class="option"><input type="radio" name="answer-{{ question.id }}" value="3">{{ question.Option3 }}</p>
<p class="option"><input type="radio" name="answer-{{ question.id }}" value="4">{{ question.Option4 }}</p>
{% endfor %}
<button type="submit">End</button>
</form>
答案 0 :(得分:0)
将它们放在列表中
foo = [ques1, ques2, ques3, ques4]
list_x = random.sample(foo, len(foo))