How can I set a field value in templates?

I’m trying to write a form for a basic multiple choice quiz. I’ve tried writing the form manually on my template and it’s fine but I would like to use a django form instead. I’m trying to assign the options for the form on my template but I can’t seem to find the correct way of doing this anywhere so I’d appreciate some help

class Quiz(models.Model):
    quiz_id = models.AutoField(primary_key=True)
    quiz_name = models.TextField()

class QuizQuestion(models.Model):
    question_id = models.AutoField(primary_key=True)
    question_text = models.TextField()
    quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE)

class QuestionOption(models.Model):
    option_id = models.AutoField(primary_key=True)
    option_text = models.TextField()
    is_correct = models.BooleanField()
    question = models.ForeignKey(QuizQuestion, on_delete=models.CASCADE)

class QuizForm(forms.Form):
    fields = ['options']

template (form written manually)

<div class="container">
        <form method="POST" class="mt-2">
            {% for question in all_questions %}
                {{ question.question_text }}
                {% for option in question.questionoption_set.all %}
                    <input type="radio" id="option_{{option.option_id}}" value="{{option.option_text}}">
                    <label for="option_{{option.option_id}}">{{option.option_text}}</label>
                {% endfor %}
            {% endfor %}
            <center><button class="btn btn-custom mt-2" type="submit">SUBMIT</button></center>

I’ve tried using:

{% for option in question.questionoption_set.all %}
      {{ quizform }}
      {{ quizform.options(value=option.option_text) }}
{% endfor %}

but I got a TemplateSyntaxError

So in short, I would like to know what the correct syntax would be / the correct way of writing this form

Thanks in advance :))

What you are looking for is a ChoiceField for your questions. You can then define a function to return the set of choices to be used for each question.

Also, since you are rendering multiple instances of essentials the same form, you want to build this page using a formset.

I’ve tried using formsets as you advised, but I’m getting a KeyError.

def quiz_ongoing(request, quiz_id):
    chosenquiz_id = quiz_id
    all_questions = QuizQuestion.objects.filter(quiz_id=chosenquiz_id)
    for question in all_questions:
        curr_question = question.question_id
        option = QuestionOption.objects.filter(question_id=curr_question).values_list('option_text', flat=True)
        quizform = QuizForm(options=option)
QuizFormSet = formset_factory(QuizForm)
quizformset = QuizFormSet()
return render(request, 'quiz_ongoing.html', {'quizformset': quizformset})

class QuizForm(forms.Form):
    options = forms.MultipleChoiceField()

    def __init__(self, *args, **kwargs):
        options = kwargs.pop('options')
        super(QuizForm, self).__init__(*args, **kwargs)
        self.fields['options'] = forms.MultipleChoiceField(choices = [(o, str(o)) for o in options], widget=RadioSelect)


    <div class="container">
        <form method="POST" class="mt-2">
            {% csrf_token %}
            {{ quizformset.management_form }}
            {% for form in quizformset %}
                {{ form }}
            {% endfor %}
            <center><button class="btn btn-custom mt-2" type="submit">SUBMIT</button></center>

The error:
Error during template rendering
Exception Type: KeyError at /assignments/quiz_ongoing/2
Exception Value: ‘options’

Thanks again

None of this:

does you any good and can all be deleted.

(Side note - I’m hoping this is a copy/paste error, but these lines:

don’t show up as being indented in your view. If they’re not indented as part of your view, that is a mistake.)

The purpose of the formset is to allow the formset to generate the individual forms, including the construction of any specialization that may be necessary.

In the section for QuizForm, I would recommend changing one of these variables named “options” to something else, just to avoid confusion. (Your “You in six months” will appreciate it.)

For actually creating “dynamic options” for forms, I suggest you read:

And you may also want to read the thread at Forms fields based on database content