|
| 1 | +from django import forms |
| 2 | + |
| 3 | +class NameForm(forms.Form): |
| 4 | + name = forms.CharField(label='What is your name?', max_length=100) |
| 5 | + |
| 6 | +class QuizForm(forms.Form): |
| 7 | + QUESTIONS = { |
| 8 | + 1: { |
| 9 | + 'label': "Who is known as the father of Computer?", |
| 10 | + 'choices': [('a', 'Alan Turing'), ('b', 'Charles Babbage'), ('c', 'John von Neumann'), ('d', 'Ada Lovelace')] |
| 11 | + }, |
| 12 | + 2: { |
| 13 | + 'label': "Who is the author of 'Pride and Prejudice'?", |
| 14 | + 'choices': [('a', 'Emily Brontë'), ('b', 'Charles Dickens'), ('c', 'Jane Austen'), ('d', 'Mark Twain')] |
| 15 | + }, |
| 16 | + 3: { |
| 17 | + 'label': "What character have both Robert Downey Jr. and Benedict Cumberbatch played?", |
| 18 | + 'choices': [('a', 'Iron Man'), ('b', 'Sherlock Holmes'), ('c', 'Dr. Strange'), ('d', 'James Bond')] |
| 19 | + }, |
| 20 | + 4: { |
| 21 | + 'label': "Which planet in the Milky Way is the hottest?", |
| 22 | + 'choices': [('a', 'Venus'), ('b', 'Mars'), ('c', 'Saturn'), ('d', 'Jupiter')] |
| 23 | + }, |
| 24 | + 5: { |
| 25 | + 'label': "What city is known as The Eternal City?", |
| 26 | + 'choices': [('a', 'Athens'), ('b', 'Rome'), ('c', 'Paris'), ('d', 'Cairo')] |
| 27 | + }, |
| 28 | + 6: { |
| 29 | + 'label': "Who discovered that the earth revolves around the sun?", |
| 30 | + 'choices': [('a', 'Galileo Galilei'), ('b', 'Isaac Newton'), ('c', 'Nicolaus Copernicus'), ('d', 'Johannes Kepler')] |
| 31 | + }, |
| 32 | + 7: { |
| 33 | + 'label': "What sports car company manufactures the 911?", |
| 34 | + 'choices': [('a', 'Ferrari'), ('b', 'Lamborgini'), ('c', 'Porsche'), ('d', 'Buggati')] |
| 35 | + }, |
| 36 | + 8: { |
| 37 | + 'label': "Which planet has the most moons?", |
| 38 | + 'choices': [('a', 'Venus'), ('b', 'Mars'), ('c', 'Saturn'), ('d', 'Jupiter')] |
| 39 | + }, |
| 40 | + 9: { |
| 41 | + 'label': "How many bones do we have in an ear?", |
| 42 | + 'choices': [('a', '2'), ('b', '3'), ('c', '4'), ('d', '5')] |
| 43 | + }, |
| 44 | + 10: { |
| 45 | + 'label': "What software company is headquartered in Redmond, Washington?", |
| 46 | + 'choices': [('a', 'Apple'), ('b', 'Google'), ('c', 'Microsoft'), ('d', 'Amazon')] |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + def __init__(self, *args, current_question=None, **kwargs): |
| 51 | + super().__init__(*args, **kwargs) |
| 52 | + if current_question and current_question in self.QUESTIONS: |
| 53 | + question_data = self.QUESTIONS[current_question] |
| 54 | + self.fields[f'q{current_question}'] = forms.ChoiceField( |
| 55 | + label=question_data['label'], |
| 56 | + choices=question_data['choices'], |
| 57 | + widget=forms.RadioSelect |
| 58 | + ) |
0 commit comments