回答編集履歴
1
削除
test
CHANGED
@@ -1,145 +1 @@
|
|
1
|
-
選択肢フォームは最大5個で、それが2個以上埋まってればモデルを生成するという方法をとりました。
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
```Python
|
6
|
-
|
7
|
-
# form.py
|
8
|
-
|
9
|
-
class ChoiceForm(forms.Form):
|
10
|
-
|
11
|
-
choice1 = forms.CharField(label="選択肢1", max_length=200, required=True,)
|
12
|
-
|
13
|
-
choice2 = forms.CharField(label="選択肢2", max_length=200, required=True,)
|
14
|
-
|
15
|
-
choice3 = forms.CharField(label="選択肢3", max_length=200, required=False,)
|
16
|
-
|
17
|
-
choice4 = forms.CharField(label="選択肢4", max_length=200, required=False,)
|
18
|
-
|
19
|
-
choice5 = forms.CharField(label="選択肢5", max_length=200, required=False,)
|
20
|
-
|
21
|
-
```
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
```Python
|
26
|
-
|
27
|
-
# view.py
|
28
|
-
|
29
|
-
def question_create(request):
|
30
|
-
|
31
|
-
if request.method == "POST":
|
32
|
-
|
33
|
-
form1 = QuestionForm(request.POST)
|
34
|
-
|
35
|
-
form2 = ChoiceForm(request.POST)
|
36
|
-
|
37
|
-
if form1.is_valid() and form2.is_valid():
|
38
|
-
|
39
|
-
question = form1.save(commit=False)
|
40
|
-
|
41
|
-
question.pub_date = timezone.now()
|
42
|
-
|
43
|
-
question.close_date = timezone.datetime.now() + timezone.timedelta(weeks=1)
|
44
|
-
|
45
|
-
question.save()
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
question.choice_set.create(
|
50
|
-
|
51
|
-
choice_text=form2.cleaned_data.get("choice1"), votes=0
|
52
|
-
|
53
|
-
)
|
54
|
-
|
55
|
-
question.choice_set.create(
|
56
|
-
|
57
|
-
choice_text=form2.cleaned_data.get("choice2"), votes=0
|
58
|
-
|
59
|
-
)
|
60
|
-
|
61
|
-
if form2.cleaned_data.get("choice3") != "":
|
62
|
-
|
63
|
-
question.choice_set.create(
|
64
|
-
|
65
|
-
choice_text=form2.cleaned_data.get("choice3"), votes=0
|
66
|
-
|
67
|
-
)
|
68
|
-
|
69
|
-
if form2.cleaned_data.get("choice4") != "":
|
70
|
-
|
71
|
-
question.choice_set.create(
|
72
|
-
|
73
|
-
choice_text=form2.cleaned_data.get("choice4"), votes=0
|
74
|
-
|
75
|
-
)
|
76
|
-
|
77
|
-
if form2.cleaned_data.get("choice5") != "":
|
78
|
-
|
79
|
-
question.choice_set.create(
|
80
|
-
|
81
|
-
choice_text=form2.cleaned_data.get("choice5"), votes=0
|
82
|
-
|
83
|
-
)
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
question.save()
|
88
|
-
|
89
|
-
return redirect("polls:_votes")
|
90
|
-
|
91
|
-
else:
|
92
|
-
|
93
|
-
form1 = QuestionForm()
|
94
|
-
|
95
|
-
form2 = ChoiceForm()
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
context = {
|
100
|
-
|
101
|
-
"form1": form1,
|
102
|
-
|
103
|
-
"form2": form2,
|
104
|
-
|
105
|
-
}
|
106
|
-
|
107
|
-
return render(request, "polls/create/index.html", context)
|
108
|
-
|
109
|
-
```
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
```html
|
114
|
-
|
115
|
-
<form method="POST">{% csrf_token %}
|
116
|
-
|
117
|
-
{{ form1.as_p }}
|
118
|
-
|
119
|
-
|
1
|
+
----------
|
120
|
-
|
121
|
-
{{ form2.choice1.label }}:{{ form2.choice1 }}
|
122
|
-
|
123
|
-
<hr>
|
124
|
-
|
125
|
-
{{ form2.choice2.label }}:{{ form2.choice2 }}
|
126
|
-
|
127
|
-
<hr>
|
128
|
-
|
129
|
-
{{ form2.choice3.label }}:{{ form2.choice3 }}
|
130
|
-
|
131
|
-
<hr>
|
132
|
-
|
133
|
-
{{ form2.choice4.label }}:{{ form2.choice4 }}
|
134
|
-
|
135
|
-
<hr>
|
136
|
-
|
137
|
-
{{ form2.choice5.label }}:{{ form2.choice5 }}
|
138
|
-
|
139
|
-
<hr>
|
140
|
-
|
141
|
-
<button type="submit">Save</button>
|
142
|
-
|
143
|
-
</form>
|
144
|
-
|
145
|
-
```
|