django радиокнопка по запросу. POST не работает - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь прочитать значение из radio.input в приведенной ниже форме, используя

def question_detail(request,question_id,quiz_id):
q = Quiz.objects.get(pk = quiz_id)
que = Question.objects.get(pk = question_id)
count = q.question_set.count()
try:
    selected_choice = que.answer_set.get(pk=request.POST['choice'])
except(KeyError, Answer.DoesNotExist):
    come = que.rank
    came = come + 1
    later_question = q.question_set.get(rank=came)
    return render(request, 'app/question_detail.html',{'que': que, 'count': count, 'later_question': later_question})
else:
    if selected_choice.correct is True:
        try:
            come = que.rank
            came = come + 1
            later_question = q.question_set.get(rank=came)
        except:
            come = que.rank
            came = come
            later_question = q.question_set.get(rank=came)
    else:
        come = que.rank
        later_question = q.question_set.get(rank=come)
    return render(request, 'app/question_detail.html',{'count': count, 'que': que, 'later_question': later_question})

, но когда я пытаюсь получить доступ к веб-странице, он выдает Keyerror, пожалуйста, помогите

<form action="{% url 'app:detail' quiz_id=que.quiz.id question_id=later_question.id%}" method="post">{% csrf_token%}
{% for choice in que.answer_set.all %}
    <input type="radio" name='choice' value="{{choice.id}}">
    <label>{{choice.answer}}-{{choice.correct}}-{{choice.rank}}</label><br>
{% endfor %}

...