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:
- How is my restaurant
- How is the food
- 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
Step back and think it clearly through–you’ll need 3
ChoiceFieldto 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: