Skip to content Skip to sidebar Skip to footer

Django "dynamic" Verification Form

I'm trying to create a verification form in Django that presents a user with a list of choices, only one of which is valid. For example, for a user whose favourite pizza toppings i

Solution 1:

You will need to build up the choices in the the form init method, otherwise the form elements choices will be the same each time the page loads (they won't change).

class QuestionForm(Form):
    questions = ChoiceField()

    def __init__(self, *args, **kwargs):
        super(QuestionForm, self).__init__(*args, **kwargs)
        self.fields['questions'].choices = set_up_choices() # function that creates list

    clean_questions(self):
        # do your validation of the question selection
        # here you could check whether the option selected matches
        # a correct value, if not throw a form validation exception
        return self.cleaned['questions']

Post a Comment for "Django "dynamic" Verification Form"