質問編集履歴
3
文字
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,9 +30,7 @@
|
|
30
30
|
|
31
31
|
|
32
32
|
|
33
|
-
|
33
|
+
|
34
|
-
|
35
|
-
https://github.com/taroimagawa/money
|
36
34
|
|
37
35
|
|
38
36
|
|
2
文字
test
CHANGED
File without changes
|
test
CHANGED
@@ -49,3 +49,277 @@
|
|
49
49
|
python 3.9.1
|
50
50
|
|
51
51
|
django 3.1.5
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
###追記
|
60
|
+
|
61
|
+
画像を用いて質問したかったので、もう一度投稿させていただきます。
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
金額を入力して割り勘金額を動的に表示するプログラムを実装中です。
|
66
|
+
|
67
|
+
画像の赤線部分が今回解決したいエラー箇所です。
|
68
|
+
|
69
|
+
![イメージ説明](55865b42953eef99fae0d2779f1d2a67.png)
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
### 該当のソースコード(views.py)
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
class MoneyListView(ListView):
|
84
|
+
|
85
|
+
model = Money
|
86
|
+
|
87
|
+
context_object_name = 'money_list'
|
88
|
+
|
89
|
+
paginate_by = 3
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
#db aggregate
|
96
|
+
|
97
|
+
def get_context_data(self, **kwargs):
|
98
|
+
|
99
|
+
context = super().get_context_data(**kwargs)
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
#sum
|
104
|
+
|
105
|
+
total_amount = Money.objects.aggregate(Sum('amount'))['amount__sum']
|
106
|
+
|
107
|
+
context["total_amount"] = total_amount
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
#average
|
112
|
+
|
113
|
+
avg_amount = Money.objects.aggregate(Sum('amount'))['amount__sum']/3
|
114
|
+
|
115
|
+
context["avg_amount"] = avg_amount
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
#payment/per name=Taro
|
120
|
+
|
121
|
+
pay_taro = Money.objects.filter(name='Taro').aggregate(Sum('amount'))['amount__sum']
|
122
|
+
|
123
|
+
context["pay_taro"] = pay_taro
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
#payment/per name=Koki
|
128
|
+
|
129
|
+
pay_koki = Money.objects.filter(name='Koki').aggregate(Sum('amount'))['amount__sum']
|
130
|
+
|
131
|
+
context["pay_koki"] = pay_koki
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
#payment/per name=Kaze
|
136
|
+
|
137
|
+
pay_kaze = Money.objects.filter(name='Kaze').aggregate(Sum('amount'))['amount__sum']
|
138
|
+
|
139
|
+
context["pay_kaze"] = pay_kaze
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
#deficiency/per name=Taro
|
144
|
+
|
145
|
+
deficiency_taro = avg_amount - pay_taro
|
146
|
+
|
147
|
+
context["deficiency_taro"] = deficiency_taro
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
#deficiency/per name=Koki
|
152
|
+
|
153
|
+
deficiency_koki = avg_amount - pay_koki
|
154
|
+
|
155
|
+
context["deficiency_koki"] = deficiency_koki
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
#deficiency/per name=Kaze
|
160
|
+
|
161
|
+
deficiency_kaze = avg_amount - pay_kaze
|
162
|
+
|
163
|
+
context["deficiency_kaze"] = deficiency_kaze
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
#create graph
|
168
|
+
|
169
|
+
x_list = ['Taro', 'Koki', 'Kaze']
|
170
|
+
|
171
|
+
y_list = [pay_taro, pay_koki, pay_kaze]
|
172
|
+
|
173
|
+
create_graph(x_list, y_list)
|
174
|
+
|
175
|
+
graph = get_image()
|
176
|
+
|
177
|
+
context['graph'] = graph
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
return context
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
```
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
### 該当のソースコード(htmlファイル)
|
190
|
+
|
191
|
+
```
|
192
|
+
|
193
|
+
{% extends 'base.html' %}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
{% block body %}
|
198
|
+
|
199
|
+
<div class="container">
|
200
|
+
|
201
|
+
<p><a href="{% url 'create' %}" class="btn btn-secondary">追加</a></p>
|
202
|
+
|
203
|
+
{% for money in object_list %}
|
204
|
+
|
205
|
+
<div class="alert alert-secondary" role="alert">
|
206
|
+
|
207
|
+
<p>{{ money.purpose }}</p>
|
208
|
+
|
209
|
+
<p> {{ money.name }} : {{ money.amount }}円 </p>
|
210
|
+
|
211
|
+
<a href="{% url 'detail' money.pk %}" class="btn btn-secondary btn-sm" tabindex="-1" role="button" aria-disabled="true">詳細</a>
|
212
|
+
|
213
|
+
<a href="{% url 'update' money.pk %}" class="btn btn-secondary btn-sm" tabindex="-1" role="button" aria-disabled="true">編集</a>
|
214
|
+
|
215
|
+
<a href="{% url 'delete' money.pk %}" class="btn btn-secondary btn-sm" tabindex="-1" role="button" aria-disabled="true">削除</a>
|
216
|
+
|
217
|
+
</div>
|
218
|
+
|
219
|
+
{% endfor %}
|
220
|
+
|
221
|
+
<div class="alert alert-primary" role="alert">
|
222
|
+
|
223
|
+
<h6>総合計金額</h6>
|
224
|
+
|
225
|
+
<p>{{ total_amount }}円</p>
|
226
|
+
|
227
|
+
<h6>割り勘金額</h6>
|
228
|
+
|
229
|
+
<p>{{ avg_amount|floatformat:1 }}円</p>
|
230
|
+
|
231
|
+
<h6>現在の支払金額</h6>
|
232
|
+
|
233
|
+
<p>Taro:{{ pay_taro }}円<br>
|
234
|
+
|
235
|
+
Koki:{{ pay_koki }}円<br>
|
236
|
+
|
237
|
+
Kaze:{{ pay_kaze }}円</p>
|
238
|
+
|
239
|
+
<h6>支払い不足金額</h6>
|
240
|
+
|
241
|
+
<p>Taro:{{ deficiency_taro|floatformat:1 }}円<br>
|
242
|
+
|
243
|
+
Koki:{{ deficiency_koki|floatformat:1 }}円<br>
|
244
|
+
|
245
|
+
Kaze:{{ deficiency_kaze|floatformat:1 }}円</p>
|
246
|
+
|
247
|
+
<p>
|
248
|
+
|
249
|
+
***<br>
|
250
|
+
|
251
|
+
〜支払い不足金額について〜<br>
|
252
|
+
|
253
|
+
+n円:n円払ってもらい過ぎ<br>
|
254
|
+
|
255
|
+
-n円:n円払い過ぎ<br>
|
256
|
+
|
257
|
+
***
|
258
|
+
|
259
|
+
</p>
|
260
|
+
|
261
|
+
</div>
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
<img src="data:image/png;base64, {{ graph | safe }} " alt="">
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
<nav aria-label="Page navigation example">
|
270
|
+
|
271
|
+
<ul class="pagination">
|
272
|
+
|
273
|
+
{% if page_obj.has_previous %}
|
274
|
+
|
275
|
+
<li class="page-item">
|
276
|
+
|
277
|
+
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
|
278
|
+
|
279
|
+
<span aria-hidden="true">«</span>
|
280
|
+
|
281
|
+
<span class="sr-only">Previous</span>
|
282
|
+
|
283
|
+
</a>
|
284
|
+
|
285
|
+
</li>
|
286
|
+
|
287
|
+
{% endif %}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
{% for page in paginator.page_range %}
|
292
|
+
|
293
|
+
<li {% if page == page_obj.number %} class="page-item active" {% endif %}><a class="page-link" href="?page={{ page }}">{{ page }}</a></li>
|
294
|
+
|
295
|
+
{% endfor %}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
{% if page_obj.has_next %}
|
300
|
+
|
301
|
+
<li class="page-item">
|
302
|
+
|
303
|
+
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
|
304
|
+
|
305
|
+
<span aria-hidden="true">»</span>
|
306
|
+
|
307
|
+
<span class="sr-only">Next</span>
|
308
|
+
|
309
|
+
</a>
|
310
|
+
|
311
|
+
</li>
|
312
|
+
|
313
|
+
{% endif %}
|
314
|
+
|
315
|
+
</ul>
|
316
|
+
|
317
|
+
</nav>
|
318
|
+
|
319
|
+
</div>
|
320
|
+
|
321
|
+
{% endblock %}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
```
|
1
質問の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,9 +6,15 @@
|
|
6
6
|
|
7
7
|
ランサーバーを起動すると正常に動くのですが、テストコードを実行するとエラーが発生します。
|
8
8
|
|
9
|
+
初歩的な質問かもしれませんが、解決策をご教授していただけないでしょうか。
|
10
|
+
|
9
11
|
|
10
12
|
|
11
13
|
### 発生している問題・エラーメッセージ
|
14
|
+
|
15
|
+
型を合わせて計算する方法がわかりません。
|
16
|
+
|
17
|
+
データベースの集計した値を avg_amount = Money.objects.aggregate(Sum('amount'))['amount__sum']/3 と代入したのですが、TypeError: unsupported operand type(s) for /: 'NoneType' and 'int' とエラーが出ました。
|
12
18
|
|
13
19
|
|
14
20
|
|