英文:
How can i make shuffle in django Forms?
问题
我有一个类似于问答游戏的项目,但我需要在问题中对答案进行随机排序。
这是我的代码:
template.html
<form method="post">
{% csrf_token %}
<h3>{{ current_question.text }}</h3>
{{ form.selected_answer }}
<button type="submit">Next</button>
</form>
views.py
if request.method == 'POST':
form = QuestionForm(request.POST, question=current_question)
if form.is_valid():
user_answer = form.cleaned_data['selected_answer']
user_test.useranswer_set.update_or_create(question=current_question,
defaults={'selected_answer': user_answer})
return redirect('test', test_id=test_id)
else:
form = QuestionForm(question=current_question)
这是我的Django表单。我尝试了这样的方式,但它不起作用:
from django import forms
from .models import Answer
import random
from django.db.models.query import QuerySet
class QuestionForm(forms.Form):
selected_answer = forms.ModelChoiceField(
queryset=Answer.objects.none(),
widget=forms.RadioSelect,
empty_label=None
)
def __init__(self, *args, question=None, **kwargs):
super().__init__(*args, **kwargs)
if question:
answers = list(question.answer_set.all())
random.shuffle(answers)
self.fields['selected_answer'].queryset = answers
英文:
I have got a project like quiz. But i need to shuffle the answers in questions.
here is my code:
template.html
<form method="post">
{% csrf_token %}
<h3>{{ current_question.text }}</h3>
{{ form.selected_answer }}
<button type="submit">Next</button>
</form>
views.py
if request.method == 'POST':
form = QuestionForm(request.POST, question=current_question)
if form.is_valid():
user_answer = form.cleaned_data['selected_answer']
user_test.useranswer_set.update_or_create(question=current_question,
defaults={'selected_answer': user_answer})
return redirect('test', test_id=test_id)
else:
form = QuestionForm(question=current_question)
There is my django form. I try like this but it doesn't work:
from django import forms
from .models import Answer
import random
from django.db.models.query import QuerySet
class QuestionForm(forms.Form):
selected_answer = forms.ModelChoiceField(
queryset=Answer.objects.none(),
widget=forms.RadioSelect,
empty_label=None
)
def __init__(self, *args, question=None, **kwargs):
super().__init__(*args, **kwargs)
if question:
answers = list(question.answer_set.all())
random.shuffle(answers)
self.fields['selected_answer'].queryset = answers
答案1
得分: 1
如果答案的数量很少,这里可能是这种情况,你可以使用.order_by('?')
来处理。
class QuestionForm(forms.Form):
selected_answer = forms.ModelChoiceField(
queryset=Answer.objects.none(), widget=forms.RadioSelect, empty_label=None
)
def __init__(self, *args, question=None, **kwargs):
super().__init__(*args, **kwargs)
if question:
self.fields[
'selected_answer'
].queryset = question.answer_set<b>.order_by('?')</b>
英文:
If the number of answers is small, which is likely the case here, you can work with .order_by('?')
<sup>[Django-doc]</sup>:
<pre><code>class QuestionForm(forms.Form):
selected_answer = forms.ModelChoiceField(
queryset=Answer.objects.none(), widget=forms.RadioSelect, empty_label=None
)
def __init__(self, *args, question=None, **kwargs):
super().__init__(*args, **kwargs)
if question:
self.fields[
'selected_answer'
].queryset = question.answer_set<b>.order_by('?')</b></code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论