/ students / exam / 1 /上的MultiValueDictKeyError'choice_pk2'

时间:2019-04-11 07:12:10

标签: django

我通过单选按钮循环以获取所选择选项的值。但是,每当未选择选项时,我都会收到此错误。如何将取消选择选项发送给 NULL 或False。

try:
    choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_pk]
except MultiValueDictKeyError:
    choice_pk = False

if request.method == 'POST':

     question_pk = request.POST.getlist('question_pk', False)             
     choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_pk]
     '''
     try:
        choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_pk]
    except MultiValueDictKeyError:
        choice_pk = False
    '''   

    #print(marks_obtainable.marks_obtained)
    #zipped = zip(question_pk, choice_pk)
    with transaction.atomic()

enter image description here

0 个答案:

没有答案