如何验证具有相同名称的空条目Django多个输入字段值

时间:2019-03-01 07:40:28

标签: django-forms

使用问题Django multiple input field values with same name中提到的这种方法后,但是每当我不选择单选按钮中的值时,就会出错。但是,我还会问如何将值添加到数据库中,因为问题是答案的一个实例。

enter image description here

@login_required
@student_required
def take_exam(request, pk):
    course = get_object_or_404(Course, pk=pk)
    student = request.user.student
    question = course.questions.filter()  
    #correct_answers = student.course_answers.filter(answer__question__quiz=course, answer__is_correct=True).count()
    total_questions = course.questions.count()
    choice = Answer.objects.filter()
    marks_obtainable = Details.objects.get(course_id=course)



    if request.method == 'POST':

        question_pk = request.POST.getlist('question_pk')
        choice_pk = [request.POST['choice_pk{}'.format(q)] for q in question_pk]


        zipped = zip(question_pk, choice_pk)

        for x, y in zipped:
            correct_answers = Answer.objects.filter(question_id=x,  is_correct=True).values('id').first()['id']
            #print(type(correct_answers))  
            #print(choice_pk) 
            print(x, y, correct_answers)        


            print(x, y, correct_answers)
            if int(y) == correct_answers:
                print("correct")
                z = TakenQuiz.objects.create(student=student, question=int(x), course=course, mark_obtained=marks_obtainable, is_correct=True)
                z.save()
                '''
                takenquiz = TakenQuiz()
                takenquiz.student = student
                takenquiz.question = x
                takenquiz.course = course
                takenquiz.selected_choice = y
                takenquiz.marks_obtained = marks_obtainable
                takenquiz.is_correct = True
                takenquiz.save()
                '''
            else:
                print("Not correct")
                z = TakenQuiz.objects.create(student=student, question=x, course=course, mark_obtained=marks_obtainable, is_correct=False)
                z.save()







    return render(request, 'classroom/students/take_exam_form.html', {
        'course': course,
        'question': question,
        'course': course,
        'total_questions': total_questions,
        'choice': choice,
        'marks_obtainable': marks_obtainable,

    })

take_exam_form.html

<form method="post" novalidate>

        {% csrf_token %}

        {% for questions in question %}

        <input type="hidden" name="question_pk" value="{{ questions.pk }}">

        <h3 class="text-info">{{ questions.text|safe }}</h3>



    {% for choices in questions.answers.all %}





        <input class="form-check-input" type="radio" name="choice_pk{{ questions.pk }}" id="choices-{{ forloop.counter }}" value="{{ choices.pk }}">

        <label class="form-check-label" for="choices-{{ forloop.counter }}">
              {{ choices.text|safe }}
        </label>


      {% endfor %}


      {% endfor %}
            <button type="submit" class="btn btn-primary">Submit Now →</button>


</form>

0 个答案:

没有答案