1

Here is what I have, a simple Django form

class survey (forms.Form):
    answer = forms.ChoiceField(
                                 widget = RadioSelect(),
                                 choices = answers_select
                                )

Now, on my HTML page, I got not just one question but many! Is it possible to use the above answer field for all the questions? For all the questions, its just the same choices I have to show!

Say I have 3 questions:

  1. How is my restaurant
  2. How is the food
  3. How is the service

choices for the above answer field are 1. good, 2. bad 3. worst So, I don't want to create 3 form fields for the 3 questions as its redundant

2 Answers 2

2

Step back and think it clearly through--you'll need 3 ChoiceField to track the answer for 3 separate questions and there's no way around it.

What would be redundant is to actually repeat the form field construction call, especially if you were dealing with, say, 20 questions. In this case, rather than statically constructing those fields, you can store the list of questions as a class invariant and create the form fields dynamically during the forms construction.

Here's something to give you a starting idea on how you might go about doing it:

class SurveyForm(forms.Form):
    questions = _create_questions('How is my restaurant?',
                                  'How is the Food?',
                                  'How is the service?')

    def __init__(self, *args, **kwargs):
        # Create the form as usual
        super(SurveyForm, self).__init__(*args, **kwargs)

        # Add custom form fields dynamically
        for question in questions:
             self.fields[question[0]] = forms.ChoiceField(label=question[1],
                                                          widget=forms.RadioSelect(),
                                                          choices=answers_select)
    @classmethod
    def _create_questions(cls, *questions):
        return [(str(index), question) for index, question in enumerate(questions)]
Sign up to request clarification or add additional context in comments.

Comments

1

You're looking for formsets. You could do something like this:

from django.forms.formsets import formset_factory
SurveyFormSet = formset_factory(survey, extra=3, max_num=3)

Add it to your context:

def get_context_data(self, request, *args, **kwargs):
    data = super(MyView, self).get_context_data(request, *args, **kwargs)
    data['formset'] = SurveyFormSet()
    return data

Then use it in the template:

<form method="post" action="">
    {{ formset.management_form }}
    <table>
        {% for form in formset %}
        {{ form }}
        {% endfor %}
    </table>
</form>

During a post, you'll want to pass request.POST and request.FILES into the formset constructor:

formset = SurveyFormSet(request.POST, request.FILES)

It's all described pretty thoroughly in that linked documentation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.