用Django做问卷调查 如何实现随机10个不重复的问题

HesterG 2016-08-20 09:10:17
model.py:

class Quiz(models.Model):
QuizUrl = models.CharField(max_length=200)
......

class Question(models.Model):
Quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)
......

view.py:
def quizWelcome(request, quiz_url):
cur_quiz = get_object_or_404(Quiz, QuizUrl=quiz_url)
if request.method == 'GET':
time_clicked_str = request.COOKIES.get('clicked')
print(time_clicked_str)
time_clicked = 0
time_clicked_str = request.COOKIES.get('clicked')
elif request.method == 'POST':
time_clicked_str = request.COOKIES.get('clicked')
time_clicked = int(time_clicked_str)
answer_id_selected = request.POST.get("answer", "")
answer_selected = get_object_or_404(Answer, pk=int(answer_id_selected))
if (answer_selected.IsCorrectAnswer):
time_clicked += 1
next_question = random.choice(cur_quiz.question_set.all())
context = {'cur_quiz': cur_quiz, 'time_clicked': time_clicked, 'this_question': next_question}
response = render(request, 'quiz_welcome.html', context)
response.set_cookie('clicked', time_clicked)
return response

quiz_welcome.html:
<h1> Get Ready For Quiz </h1>

<body>
<p> {{cur_quiz.QuizName}} </p>
<p> {{this_question.QuestionText}} </p>
<form action = "{%url 'funnyquiz:quiz_welcome' cur_quiz.QuizUrl%}"
method = "post">
{% csrf_token %}
{% for answer in this_question.answer_set.all %}
<button type="submit" name="answer" value="{{answer.id}}"> {{answer.AnswerText}} </button>
</body>


目前只实现了随机 而且还是重复的
求大神指点如何随机10题 并且不重复..

...全文
324 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

10,607

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 其他
社区管理员
  • 其他
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧