Python 如何在Django模板中将类中的choices参数用作radio输入值
我想知道如何定义Django模板中单选按钮的选项值。 这是我的型号。pyPython 如何在Django模板中将类中的choices参数用作radio输入值,python,django,Python,Django,我想知道如何定义Django模板中单选按钮的选项值。 这是我的型号。py RATING_CHOICES = ((1, "Weak"), (2, "Average"), (3, "Good"), (4, "Excellent")) ... class Answer(models.Model): teacher = models.ForeignKey(Teacher, on_delete=models.CASCA
RATING_CHOICES = ((1, "Weak"), (2, "Average"), (3, "Good"), (4, "Excellent"))
...
class Answer(models.Model):
teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
answer = models.SmallIntegerField(choices=RATING_CHOICES, default=1)
def index(request):
context = { "questions": Question.objects.all(),
"answers": Answer.objects.all(),
"departments": Department.objects.all(),
"semesters": Semester.objects.all(),
"teachers": Teacher.objects.all(),
"subjects": Subject.objects.all(),}
return render(request, "evaluation/index.html", context)
在模板中,我想将我的RARING_选项的值分配给循环条件中的radio
{% for question in questions %}
<li>{{ question }}</li>
<ul>
<input type="radio" value="">
<input type="radio" value="">
<input type="radio" value="">
<input type="radio" value="">
</ul>
{% endfor %}
从视图传递上下文中的RATING_选择值
RATING_CHOICES = ((1, "Weak"), (2, "Average"), (3, "Good"), (4, "Excellent"))
context = { "questions": Question.objects.all(),
"answers": Answer.objects.all(),
"departments": Department.objects.all(),
"semesters": Semester.objects.all(),
"teachers": Teacher.objects.all(),
"subjects": Subject.objects.all(),
"rating_choices":RATING_CHOICES,
}
在模板中,在评分选项上运行for循环
{% for question in questions %}
<li>{{ question }}</li>
<ul>
{% for choice in rating_choices %}
<input type="radio" value="{{choice.0}}">{{choice.1}}
{% endfor %}
</ul>
{% endfor %}
{%用于问题中的问题%}
{{问题}
{评分中的选项为%_choices%}
{{choice.1}}
{%endfor%}
{%endfor%}
models.py中的评级选项如何?您可以从models.py导入,就像从。models导入评级选项有一个问题,Kumar先生,当我为一个问题选择一个选项并为另一个问题选择另一个选项时,我选择的无线电开关从第一个问题切换到第二个问题?每个问题的名称都应该不同。这就好像我不知道如何在我的视图中访问name=“question”{{{{{question.id}}。另一个字段很简单,例如teacher=request.POST.get('teacher'),但这个字段对我来说很棘手。