正确提交的django表单包含用户数据,但显示为未绑定且无效

时间:2016-07-15 02:56:23

标签: django forms

我有一个带有单个ChoiceField(小部件radioselect)的表单,其选择是动态生成并完美呈现的。提交表单时,POST数据显示所选选项的正确值(1-5)(在实时调试和浏览器调试信息中),但无论出于何种原因,表单都被视为未绑定且无效。这是以下形式:

class ReValidateStudentForm(forms.Form):
    bs_answer = forms.ChoiceField(widget=RadioSelect, label='Answer', required=True)

    def __init__(self, possible_answers, *args, **kwargs):
        super(ReValidateStudentForm, self).__init__(*args, **kwargs)
        self.fields['bs_answer'].choices = possible_answers

仅供参考," possible_answers"是一个传递给表单的元组列表,在呈现新表单时作为无线电选择选项完美呈现。 request.method确实等于POST。

有什么建议吗?

修改

这是表单视图代码:

@login_required(login_url='/login/')
def ask_identity_questions(request):
    si = StudentInfo.objects.get(student=request.user)
    qnum = si.bs_question_number
    if request.method == 'POST':
        revalidateform = ReValidateStudentForm(request.POST)
        try:
            va = VerificationAnswers.objects.get(student=request.user, question_num=qnum)
        except VerificationAnswers.DoesNotExist:
            return render(request, "informational.html", {'message': 'User--question does not exist.'})
        if revalidateform.is_valid():
            va.student = request.user
            va.question_num = si.bs_question_number
            va.answer_choice = revalidateform.cleaned_data['bs_answer']
            va.question_answered_time = timezone.now()
            si.bs_question_number += 1
            si.save()
            return render(request, "informational.html", {'message': 'Thank you! Please <a href="/toc">click here </a> to continue working through the course material.'})
        else:
            return render(request, 'questions.html', {'form': revalidateform, 'question_num': qnum, 'question': va.question_text})
    else:
        if qnum == 6:
            return render(request, "informational.html", {'message': 'You have already answered all validation questions.'})
        elif si.bs_qset_id == '':
            return render(request, "informational.html", {'message': 'There is a problem with your identity validation information. Please contact customer support.'})
        else:
            # initialize blockscore account
            client = blockscore.Client({'api_key':'sk_live_149dthis3ddiseb2f5notbebforebreal'})
            si = StudentInfo.objects.get(student=request.user)
            # set question ask time
            # retrieve the user's question set using question set id stored in StudentInfo
            # retrieve specific question from set based on value in StudentInfo
            qset = client.question_sets.retrieve(si.bs_qset_id)
            qset = qset.body
            question = qset["questions"][qnum - 1].get("question")
            # add verification question and mark time asked
            try:
                va = VerificationAnswers.objects.get(student=request.user, question_num=qnum)
            except VerificationAnswers.DoesNotExist:
                va = VerificationAnswers()
                va.student = request.user
                va.question_num = qnum
                va.question_text = question
                va.question_asked_time = timezone.now()
                va.save()
            # create answer list
            possible_answers = []
            # fill answer list with tuples of answer ids + answers
            for answer in qset["questions"][qnum - 1].get("answers"):
                possible_answers.append((answer.get("id"), answer.get("answer")))
            revalidateform = ReValidateStudentForm(possible_answers)
            return render(request, 'questions.html', {'form': revalidateform, 'question_num': qnum, 'question': question})

0 个答案:

没有答案