2
votes

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

3

3 Answers

2
votes

POST <QueryDict> contains only keys of type <str>. Which means that request.POST.get(18) will return None and request.POST.get('18') will return the value (assuming that the key exists in POST <QueryDict>).

if request.method == "POST":
    for q in question:
        # convert the q.id into str
        response = request.POST.get(str(q.id))
        print(response)

OR

you may loop through the POST data and check for valid keys. Since, POST data contains other data like csrfmiddlewaretoken.

Change the value of radio button name attribute into the form of question_{{ q.id }}.

<input type="radio" id="question_{{ q.Option1 }}" name="question_{{ q.id }}" value="{{ q.Option1 }}">

Loop through POST and check if the key contains question_

if request.method == "POST":
    for k, v in request.POST.items():
        if 'question_' in k:
            # do something
1
votes

In view.py, you are using "q.id" as a HTTP param name while I think you intended it to be 11, 18, 19 etc. Try this :-

if request.method == "POST":
    for q in question:
        response = request.POST.get(q.id)
        print(response)
0
votes

I would reconfigure the Question model to use Choices:

SOME = 'win'
OTHER = 'lin'

Answer_CHOICES = (
    (SOME, 'Some Answer'),
    (OTHER, 'Other Answer'),
)

class Question(models.Model):
    ....
    Options = models.CharField(choices=Answer_CHOICES, max_length=100)
    ....

Check the Choices field and how you may use it. Also, check crispy forms which automatically handle the form input for you.