【问题标题】:How to get selected radio button choice in django form?如何以 django 形式获得选定的单选按钮选项?
【发布时间】:2018-07-24 15:09:43
【问题描述】:

我有一个表格

class ScoreUpdateForm(forms.Form):
    answer_id = forms.IntegerField()
    given_score = forms.FloatField()
    CHOICES = [('calculated_score', 'Calculated Score'),
               ('given_score', 'Given Score')]

    final_score = forms.ChoiceField(choices=CHOICES, widget=forms.RadioSelect())

这是我的模板

 <form action="/teachers/questions/{{ answer.question.id }}/score" method="post">
    {{ form.given_score }}
    {{ form.final_score }}
    <input type="submit" value="Submit"/>
</form>

这是我的看法

def score(request, pk):
        if request.method == 'POST':
            form = ScoreUpdateForm(request.POST)
            if form.is_valid():
                given_score = form.cleaned_data['given_score']
                final_score = form.cleaned_data['final_score']

我可以得到 given_score 值,但不能得到 final_score。我想在发布后在我的视图中获取所选选项的值。

【问题讨论】:

    标签: django python-3.x forms django-forms


    【解决方案1】:

    尝试使用

     final_score = form.cleaned_data['final_score']
     final_score = dict(form.fields['final_score'].choices)[final_score]
    

    【讨论】:

    • 它没有返回任何值
    猜你喜欢
    • 2012-02-19
    • 1970-01-01
    • 2011-07-28
    • 2014-04-12
    • 2012-03-12
    • 2014-08-06
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多