Ananthu Ananthu - 1 year ago 51
Python Question

working with form in Django

This is page that designed to submit the answers of my MCQ(multiple choice question) App in Django.

{% extends 'quiz/base.html' %}
{% block content%}
<h1>You are at quiz page</h1>

<form action="{% url 'quiz:process_data' %}" method="post">
{% csrf_token %}
{% for question in question_set %}
<h3>{{question.id}}.{{question.question_text }}</h3>
{% for option in question.options_set.all %}
<input type="radio" name="choice" value="{{ option.options}}" > {{option.options}}<br>
{% endfor %}
{% endfor %}
<input type="Submit" name="Submit">
</form>
{% endblock%}


The problem is that it can only select the answer of one question. There is something wrong with my form. Thank you

Answer Source
<input type="radio" name="choice" value="{{ option.options}}">

by this line name of radio button will be same for each question. that's why you are able to select only one answer. name of radio button should be different for diff. question

<input type="radio" name="choice{{question.id}}" value="{{ option.options}}">

you can use this code. In this name of radio buttons in first question will be "choices1". and you can get selected option by

request.POST.get('choices1')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download