用Django做问卷调查 如何实现随机10个不重复的问题
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题 并且不重复..