質問編集履歴
2
原因の判明
title
CHANGED
File without changes
|
body
CHANGED
@@ -73,4 +73,59 @@
|
|
73
73
|
<QueryDict: {'gender': ['on'], 'csrfmiddlewaretoken': ['yRUeAldlNxjTgBIauF8bzlrQjNk87WQfFQTQUvRyuzSNd4rNTPxHH6PgdDCAfhtQ', 'yRUeAldlNxjTgBIauF8bzlrQjNk87WQfFQTQUvRyuzSNd4rNTPxHH6PgdDCAfhtQ']}>
|
74
74
|
<Friend:id=3, jiro(0)>
|
75
75
|
|
76
|
-
よろしくお願いします。
|
76
|
+
よろしくお願いします。
|
77
|
+
|
78
|
+
### 追記
|
79
|
+
|
80
|
+
使っているのはBooleanFieldなのですが、どうやらチェックボックスはチェックされていないときはなにも送信しないようです。また同じname属性をもつformは一番最後の値が適用されるために連動してしまっているようでした。
|
81
|
+
```
|
82
|
+
class SendForm(forms.ModelForm):
|
83
|
+
class Meta:
|
84
|
+
model = Friend
|
85
|
+
fields = ['gender',]
|
86
|
+
```
|
87
|
+
|
88
|
+
強引ですが、hiddenのformではさむことで値を特定したあと、以下の関数でQueryDictをつくり直接渡してみました。もっとスマートなやり方を教えていただければ幸いです。
|
89
|
+
|
90
|
+
```
|
91
|
+
index.html
|
92
|
+
|
93
|
+
<input type="hidden" name="gender" value="/"/>
|
94
|
+
{{ Send_Form.as_p }}{% csrf_token %}
|
95
|
+
<input type="hidden" name="gender" value="/"/>
|
96
|
+
|
97
|
+
```
|
98
|
+
|
99
|
+
```
|
100
|
+
view.py
|
101
|
+
|
102
|
+
boxlist=self.checkbox_list(request.POST.getlist('gender'))
|
103
|
+
for i,o in enumerate(self.data):
|
104
|
+
Send_Form = self.Send_Form_class(boxlist[i], instance=o)
|
105
|
+
Send_Form.save()
|
106
|
+
|
107
|
+
def checkbox_list(self, list):
|
108
|
+
boxlist=[]
|
109
|
+
flag=True
|
110
|
+
next=False
|
111
|
+
on=QueryDict( "gender=on")
|
112
|
+
off=QueryDict()
|
113
|
+
#"/"ではさまれていればon "/"が二回続けばoff
|
114
|
+
for box in list:
|
115
|
+
if next:
|
116
|
+
next=False
|
117
|
+
|
118
|
+
elif box=="/" and flag:
|
119
|
+
flag=False
|
120
|
+
|
121
|
+
elif box=="/" and not flag:
|
122
|
+
boxlist.append(off)
|
123
|
+
flag=True
|
124
|
+
else:
|
125
|
+
boxlist.append(on)
|
126
|
+
flag=True
|
127
|
+
next=True
|
128
|
+
|
129
|
+
return boxlist
|
130
|
+
|
131
|
+
```
|
1
追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -38,11 +38,12 @@
|
|
38
38
|
<table class="table">
|
39
39
|
|
40
40
|
<tr>
|
41
|
+
<th class="py-1">form</th>
|
41
|
-
<th class="py-1">
|
42
|
+
<th class="py-1">gender</th>
|
42
|
-
<th class="py-1">title</th>
|
43
43
|
<th class="py-1">name</th>
|
44
|
-
<th class="py-1">
|
44
|
+
<th class="py-1">mail</th>
|
45
45
|
</tr>
|
46
|
+
<!-- ここでレコードのかずだけformをつくりたい-->
|
46
47
|
{% for item in data %}
|
47
48
|
<tr>
|
48
49
|
<td class="py-2">{{ Send_Form.as_p }}{% csrf_token %}</td>
|