I want to take value of selected radio buttons in a single page. To explain this here is my code in models.py file
class Quiz(models.Model):
Description = models.CharField(max_length=200)
def __str__(self):
return str(self.Description)
class Question(models.Model):
Question_Text = models.CharField(max_length=500)
Option1 = models.CharField(max_length=100)
Option2 = models.CharField(max_length=100)
Option3 = models.CharField(max_length=100, blank=True)
Option4 = models.CharField(max_length=100, blank=True)
Answer = models.CharField(max_length=100)
QuizID = models.ForeignKey(Quiz, on_delete=models.CASCADE)
def __str__(self):
return str(self.Question_Text)
I want user to choose one option from "Option1, Option2, Option3, Option4" fields and I want to get value of the selected radio button.
Here's my try:
disp.html
<form action="" method="post">
{% csrf_token %}
{% for q in question %}
{{ q.Question_Text }} <br>
<input type="radio" id="question_{{ q.Option1 }}" name="{{ q.id }}" value="{{ q.Option1 }}">
<label for="question_{{ q.Option1 }}">{{ q.Option1 }}</label> <br>
<input type="radio" id="question_{{ q.Option2 }}" name="{{ q.id }}" value="{{ q.Option2 }}">
<label for="question_{{ q.Option2 }}">{{ q.Option2 }}</label> <br>
{% if q.Option3 %}
<input type="radio" id="question_{{ q.Option3 }}" name="{{ q.id }}" value="{{ q.Option3 }}">
<label for="question_{{ q.Option3 }}">{{ q.Option3 }}</label> <br>
{% endif %}
{% if q.Option4 %}
<input type="radio" id="question_{{ q.Option4 }}" name="{{ q.id }}" value="{{ q.Option4 }}">
<label for="question_{{ q.Option4 }}">{{ q.Option4 }}</label> <br>
{% endif %}
{% endfor %}
<br> <input type="submit" class="btn btn-primary" value="Submit">
</form>
Now I want to get value of the selected radio button. Since the number of records may vary and hence I cannot take value of the selected radio buttons manually like:
first = request.POST.get("11")
second = request.POST.get("18")
third = request.POST.get("19")
Here 11,18 and 19 are the value of id field of records in Question Model.
So I tried for loop to do this in the following way but I am getting "None" as value.
view.py
def disp(request):
quiz = get_object_or_404(Quiz, id = 4)
question = get_list_or_404(Question, QuizID = quiz)
if request.method == "POST":
for q in question:
response = request.POST.get("q.id")
print(response)
return render(request, 'quiz/disp.html', {'question' : question, 'quiz' : quiz})
Please help me in getting the value of the selected radio button or if there's another way to get the value of selected radio button, please suggest me