質問編集履歴
6
追記しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -361,3 +361,77 @@
|
|
361
361
|
|
362
362
|
|
363
363
|
```
|
364
|
+
|
365
|
+
```html
|
366
|
+
|
367
|
+
{% extends "./_base.html" %}
|
368
|
+
|
369
|
+
{% load crispy_forms_tags %}
|
370
|
+
|
371
|
+
{% block content %}
|
372
|
+
|
373
|
+
{{ form.certifications.errors }}
|
374
|
+
|
375
|
+
<div class="container">
|
376
|
+
|
377
|
+
<div class="row">
|
378
|
+
|
379
|
+
<div class="col-12">
|
380
|
+
|
381
|
+
<h2 class="text-center">データ入力</h2>
|
382
|
+
|
383
|
+
</div>
|
384
|
+
|
385
|
+
</div>
|
386
|
+
|
387
|
+
<div class="row">
|
388
|
+
|
389
|
+
<div class="col-12">
|
390
|
+
|
391
|
+
<div class="float-right">
|
392
|
+
|
393
|
+
<a class="btn btn-outline-secondary" href="{% url 'index' %}">戻る</a>
|
394
|
+
|
395
|
+
<a class="btn btn-outline-secondary save" href="#">保存</a>
|
396
|
+
|
397
|
+
</div>
|
398
|
+
|
399
|
+
</div>
|
400
|
+
|
401
|
+
</div>
|
402
|
+
|
403
|
+
<div class="row">
|
404
|
+
|
405
|
+
<div class="col-12">
|
406
|
+
|
407
|
+
<form method="post" id="myform">
|
408
|
+
|
409
|
+
{%crispy form%}
|
410
|
+
|
411
|
+
</form>
|
412
|
+
|
413
|
+
</div>
|
414
|
+
|
415
|
+
</div>
|
416
|
+
|
417
|
+
<div class="row">
|
418
|
+
|
419
|
+
<div class="col-12">
|
420
|
+
|
421
|
+
<div class="float-right">
|
422
|
+
|
423
|
+
<a class="btn btn-outline-secondary" href="{% url 'index' %}">戻る</a>
|
424
|
+
|
425
|
+
<a class="btn btn-outline-secondary save" href="#">保存</a>
|
426
|
+
|
427
|
+
</div>
|
428
|
+
|
429
|
+
</div>
|
430
|
+
|
431
|
+
</div>
|
432
|
+
|
433
|
+
</div>
|
434
|
+
|
435
|
+
{% endblock %}
|
436
|
+
|
437
|
+
```
|
5
views.pyを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -239,3 +239,125 @@
|
|
239
239
|
{% endblock %}
|
240
240
|
|
241
241
|
```
|
242
|
+
|
243
|
+
```python
|
244
|
+
|
245
|
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
246
|
+
|
247
|
+
from django.urls import reverse_lazy
|
248
|
+
|
249
|
+
from django.views.generic import DetailView
|
250
|
+
|
251
|
+
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
252
|
+
|
253
|
+
from django_filters.views import FilterView
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
from .filters import ItemFilter
|
258
|
+
|
259
|
+
from .forms import ItemForm
|
260
|
+
|
261
|
+
from .models import Item
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
# Create your views here.
|
266
|
+
|
267
|
+
# 検索一覧画面
|
268
|
+
|
269
|
+
class ItemFilterView(LoginRequiredMixin, FilterView):
|
270
|
+
|
271
|
+
model = Item
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
# django-filter用設定
|
276
|
+
|
277
|
+
filterset_class = ItemFilter
|
278
|
+
|
279
|
+
strict = False
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
# 1ページあたりの表示件数
|
284
|
+
|
285
|
+
paginate_by = 10
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
# 検索条件をセッションに保存する
|
290
|
+
|
291
|
+
def get(self, request, **kwargs):
|
292
|
+
|
293
|
+
if request.GET:
|
294
|
+
|
295
|
+
request.session['query'] = request.GET
|
296
|
+
|
297
|
+
else:
|
298
|
+
|
299
|
+
request.GET = request.GET.copy()
|
300
|
+
|
301
|
+
if 'query' in request.session.keys():
|
302
|
+
|
303
|
+
for key in request.session['query'].keys():
|
304
|
+
|
305
|
+
request.GET[key] = request.session['query'][key]
|
306
|
+
|
307
|
+
|
308
|
+
|
309
|
+
return super().get(request, **kwargs)
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
# 詳細画面
|
316
|
+
|
317
|
+
class ItemDetailView(LoginRequiredMixin, DetailView):
|
318
|
+
|
319
|
+
model = Item
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
# 登録画面
|
326
|
+
|
327
|
+
class ItemCreateView(LoginRequiredMixin, CreateView):
|
328
|
+
|
329
|
+
model = Item
|
330
|
+
|
331
|
+
form_class = ItemForm
|
332
|
+
|
333
|
+
success_url = reverse_lazy('index')
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
# 更新画面
|
340
|
+
|
341
|
+
class ItemUpdateView(LoginRequiredMixin, UpdateView):
|
342
|
+
|
343
|
+
model = Item
|
344
|
+
|
345
|
+
form_class = ItemForm
|
346
|
+
|
347
|
+
success_url = reverse_lazy('index')
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
# 削除画面
|
354
|
+
|
355
|
+
class ItemDeleteView(LoginRequiredMixin, DeleteView):
|
356
|
+
|
357
|
+
model = Item
|
358
|
+
|
359
|
+
success_url = reverse_lazy('index')
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
```
|
4
質問事項を、少しでもわかりやすくなるよう、書き直しました。
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
django
|
1
|
+
django データベースのテキストをテンプレートで一部表示する方法
|
test
CHANGED
@@ -1,327 +1,241 @@
|
|
1
|
-
Djangoで、データベースのテキストについて、
|
1
|
+
Djangoで、データベースの①テキストを検索し(Filterをかける)、②抽出されたテキストについて、さらに③検索語の含まれる文('。'で区切られた、検索語の含まれる文)を示す方法を探しています。
|
2
|
+
|
2
|
-
|
3
|
+
○動作の例
|
4
|
+
|
5
|
+
①検索(フィルター):「蛍」
|
6
|
+
|
3
|
-
|
7
|
+
テキストを抽出「春はあけぼの。やうやう白くなりゆく山際、少し明かりて、紫だちたる雲の細くたなびきたる。夏は夜。月のころはさらなり、闇もなほ、蛍の多く飛びちがひたる。また、ただ一つ二つなど、ほのかにうち光て行くもをかし。雨など降るもをかし。」
|
8
|
+
|
4
|
-
|
9
|
+
③検索語の含まれる文を示す「月のころはさらなり、闇もなほ、蛍の多く飛びちがひたる。」
|
5
|
-
|
6
|
-
|
10
|
+
|
7
|
-
|
11
|
+
Templateタグを利用して、下記のようなプログラムをつくりました。
|
12
|
+
|
8
|
-
|
13
|
+
ただ、item_filter.htmlで検索語を関数(filter_age)に渡そうとしても、検索語が特定できずに足踏みをしています。
|
14
|
+
|
9
|
-
|
15
|
+
"filter"
|
16
|
+
|
10
|
-
|
17
|
+
form
|
18
|
+
|
11
|
-
|
19
|
+
等、片っ端から試してみたのですが、うまくいっていません(下のプログラムでは"filter"を入れていますが、動かしても検索語が含まれる文を表示するところまでは至っていません。
|
12
|
-
|
20
|
+
|
13
|
-
|
21
|
+
下記のページを読んでも、方法がわからず、お伺いする次第です。どうぞよろしくお願いいたします。
|
14
|
-
|
22
|
+
|
15
|
-
|
23
|
+
https://docs.djangoproject.com/en/2.0/howto/custom-template-tags/
|
16
|
-
|
17
|
-
・Methodによる検索条件の設定方法
|
18
|
-
|
19
|
-
[django-filterで検索用パラメータを受け取り加工してから検索する方法](https://teratail.com/questions/131200)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
たどり着いたのが、filter.pyで、Methodによりfilterに関数を付与する方法です。
|
24
|
-
|
25
|
-
しかし、実行すると「AssertionError at /
|
26
|
-
|
27
|
-
Expected 'ItemFilter.age' to return a QuerySet, but got a NoneType instead.」
|
28
|
-
|
29
|
-
というエラーメッセージが出てしまい、先に進むことができなくなってしまいました・・・。
|
30
24
|
|
31
25
|
|
32
26
|
|
33
27
|
```python
|
34
28
|
|
29
|
+
#templatetags/item_extras.py
|
30
|
+
|
35
|
-
from django import
|
31
|
+
from django import template
|
36
|
-
|
32
|
+
|
37
|
-
from .m
|
33
|
+
from django.template.defaultfilters import stringfilter
|
34
|
+
|
38
|
-
|
35
|
+
register = template.Library()
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
-
|
39
|
+
@register.filter
|
40
|
+
|
40
|
-
|
41
|
+
@stringfilter
|
42
|
+
|
41
|
-
|
43
|
+
def filter_age(value1, value2):
|
42
|
-
|
43
|
-
|
44
|
-
|
44
|
+
|
45
|
-
|
45
|
+
s = value1
|
46
|
+
|
46
|
-
|
47
|
+
f = '\n'.join(s.splitlines())
|
48
|
+
|
49
|
+
s_l = f.split('。')
|
50
|
+
|
47
|
-
|
51
|
+
word = value2
|
48
|
-
|
52
|
+
|
49
|
-
|
53
|
+
for i in s_l:
|
54
|
+
|
50
|
-
|
55
|
+
if word in i:
|
56
|
+
|
51
|
-
|
57
|
+
return i
|
52
|
-
|
53
|
-
'name': forms.TextInput(attrs={'placeholder':''}),
|
54
|
-
|
55
|
-
'age': forms.Textarea(attrs={'rows':4}),
|
56
|
-
|
57
|
-
}
|
58
58
|
|
59
59
|
```
|
60
60
|
|
61
61
|
```python
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
63
|
+
{% extends "./_base.html" %}
|
64
|
+
|
65
|
+
{% block content %}
|
66
|
+
|
67
|
+
{% load crispy_forms_tags %}
|
68
|
+
|
69
|
+
<div class="container">
|
70
|
+
|
71
|
+
<div id="myModal" class="modal fade" tabindex="-1" role="dialog">
|
72
|
+
|
73
|
+
<div class="modal-dialog" role="document">
|
74
|
+
|
75
|
+
<div class="modal-content">
|
76
|
+
|
77
|
+
<div class="modal-header">
|
78
|
+
|
79
|
+
<h5 class="modal-title">検索条件</h5>
|
80
|
+
|
81
|
+
<button type="button" class="close" data-dismiss="modal" aria-label="閉じる">
|
82
|
+
|
83
|
+
<span aria-hidden="true">×</span>
|
84
|
+
|
85
|
+
</button>
|
86
|
+
|
87
|
+
</div>
|
88
|
+
|
89
|
+
<form id="filter" method="get">
|
90
|
+
|
91
|
+
<div class="modal-body">
|
92
|
+
|
93
|
+
{{filter.form|crispy}}
|
94
|
+
|
95
|
+
</div>
|
96
|
+
|
97
|
+
</form>
|
98
|
+
|
99
|
+
<div class="modal-footer">
|
100
|
+
|
101
|
+
<a class="btn btn-outline-secondary" data-dismiss="modal">戻る</a>
|
102
|
+
|
103
|
+
<button type="submit" class="btn btn-outline-secondary" form="filter">検索</button>
|
104
|
+
|
105
|
+
</div>
|
106
|
+
|
107
|
+
</div>
|
108
|
+
|
109
|
+
</div>
|
110
|
+
|
111
|
+
</div>
|
112
|
+
|
113
|
+
<div class="row">
|
114
|
+
|
115
|
+
<div class="col-12">
|
116
|
+
|
117
|
+
<a class="btn btn-secondary filtered" style="visibility:hidden" href="/?page=1">検索を解除</a>
|
118
|
+
|
119
|
+
<div class="float-right">
|
120
|
+
|
121
|
+
<a class="btn btn-outline-secondary" href="{% url 'create' %}">新規</a>
|
122
|
+
|
123
|
+
<a class="btn btn-outline-secondary" data-toggle="modal" data-target="#myModal" href="#">検索</a>
|
124
|
+
|
125
|
+
</div>
|
126
|
+
|
127
|
+
</div>
|
128
|
+
|
129
|
+
</div>
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
<div class="row" >
|
134
|
+
|
135
|
+
<div class="col-12">
|
136
|
+
|
137
|
+
{% include "./_pagination.html" %}
|
138
|
+
|
139
|
+
</div>
|
140
|
+
|
141
|
+
</div>
|
142
|
+
|
143
|
+
<div class="row">
|
144
|
+
|
145
|
+
<ul class="list-group">
|
146
|
+
|
147
|
+
{% for item in item_list %}
|
148
|
+
|
149
|
+
<li class="list-group-item">
|
150
|
+
|
151
|
+
<div class="row">
|
152
|
+
|
153
|
+
<div class="col-3">
|
154
|
+
|
155
|
+
<p>名称</p>
|
156
|
+
|
157
|
+
</div>
|
158
|
+
|
159
|
+
<div class="col-9">
|
160
|
+
|
161
|
+
<p>{{ item.name }}</p>
|
162
|
+
|
163
|
+
</div>
|
164
|
+
|
165
|
+
</div>
|
166
|
+
|
167
|
+
<div class="row">
|
168
|
+
|
169
|
+
<div class="col-3">
|
170
|
+
|
171
|
+
<p>文</p>
|
172
|
+
|
173
|
+
</div>
|
174
|
+
|
175
|
+
<div class="col-9">
|
176
|
+
|
177
|
+
{% load item_extras %}
|
178
|
+
|
179
|
+
<p>{{ item.age | filter_age:"filter" }}</p>
|
180
|
+
|
181
|
+
</div>
|
182
|
+
|
183
|
+
</div>
|
184
|
+
|
185
|
+
<div class="row">
|
186
|
+
|
187
|
+
<div class="col-12">
|
188
|
+
|
189
|
+
<div class="float-right">
|
190
|
+
|
191
|
+
<a class="btn btn-outline-secondary " href="{% url 'detail' item.pk %}">詳細</a>
|
192
|
+
|
193
|
+
<a class="btn btn-outline-secondary " href="{% url 'update' item.pk %}">編集</a>
|
194
|
+
|
195
|
+
<a class="btn btn-outline-secondary " href="{% url 'delete' item.pk %}">削除</a>
|
196
|
+
|
197
|
+
</div>
|
198
|
+
|
199
|
+
</div>
|
200
|
+
|
201
|
+
</div>
|
202
|
+
|
203
|
+
</li>
|
204
|
+
|
205
|
+
{% empty %}
|
206
|
+
|
207
|
+
<li class="list-group-item">
|
208
|
+
|
209
|
+
対象のデータがありません
|
210
|
+
|
211
|
+
</li>
|
212
|
+
|
213
|
+
{% endfor %}
|
214
|
+
|
215
|
+
</ul>
|
216
|
+
|
217
|
+
</div>
|
218
|
+
|
219
|
+
</div>
|
220
|
+
|
221
|
+
<div class="row" >
|
222
|
+
|
223
|
+
<div class="col-12">
|
224
|
+
|
225
|
+
<div class="float-right">
|
226
|
+
|
227
|
+
<a class="btn btn-outline-secondary" href="{% url 'create' %}">新規</a>
|
228
|
+
|
229
|
+
<a class="btn btn-outline-secondary" data-toggle="modal" data-target="#myModal" href="#">検索</a>
|
230
|
+
|
231
|
+
</div>
|
232
|
+
|
233
|
+
</div>
|
234
|
+
|
235
|
+
</div>
|
236
|
+
|
237
|
+
</div>
|
238
|
+
|
239
|
+
{% endblock %}
|
118
240
|
|
119
241
|
```
|
120
|
-
|
121
|
-
```python
|
122
|
-
|
123
|
-
#views.py
|
124
|
-
|
125
|
-
from django.contrib.auth.mixins import LoginRequiredMixin
|
126
|
-
|
127
|
-
from django.urls import reverse_lazy
|
128
|
-
|
129
|
-
from django.views.generic import DetailView
|
130
|
-
|
131
|
-
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
132
|
-
|
133
|
-
from django_filters.views import FilterView
|
134
|
-
|
135
|
-
from .filters import ItemFilter
|
136
|
-
|
137
|
-
from .forms import ItemForm
|
138
|
-
|
139
|
-
from .models import Item
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
# Create your views here.
|
144
|
-
|
145
|
-
# 検索一覧画面
|
146
|
-
|
147
|
-
class ItemFilterView(LoginRequiredMixin, FilterView):
|
148
|
-
|
149
|
-
model = Item
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
# django-filter用設定
|
154
|
-
|
155
|
-
filterset_class = ItemFilter
|
156
|
-
|
157
|
-
strict = False
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
# 1ページあたりの表示件数
|
162
|
-
|
163
|
-
paginate_by = 10
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
# 検索条件をセッションに保存する
|
168
|
-
|
169
|
-
def get(self, request, **kwargs):
|
170
|
-
|
171
|
-
if request.GET:
|
172
|
-
|
173
|
-
request.session['query'] = request.GET
|
174
|
-
|
175
|
-
else:
|
176
|
-
|
177
|
-
request.GET = request.GET.copy()
|
178
|
-
|
179
|
-
if 'query' in request.session.keys():
|
180
|
-
|
181
|
-
for key in request.session['query'].keys():
|
182
|
-
|
183
|
-
request.GET[key] = request.session['query'][key]
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
return super().get(request, **kwargs)
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
# 詳細画面
|
192
|
-
|
193
|
-
class ItemDetailView(LoginRequiredMixin, DetailView):
|
194
|
-
|
195
|
-
model = Item
|
196
|
-
|
197
|
-
```
|
198
|
-
|
199
|
-
```python
|
200
|
-
|
201
|
-
#filter.py
|
202
|
-
|
203
|
-
from django_filters import FilterSet
|
204
|
-
|
205
|
-
from django_filters import filters
|
206
|
-
|
207
|
-
from .models import Item
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
class MyOrderingFilter(filters.OrderingFilter):
|
212
|
-
|
213
|
-
descending_fmt = '%s (降順)'
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
class ItemFilter(FilterSet):
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
name = filters.CharFilter(label='名称', lookup_expr='contains')
|
224
|
-
|
225
|
-
age = filters.CharFilter(method='filter_age')
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
order_by = MyOrderingFilter(
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
fields=(
|
234
|
-
|
235
|
-
('name', 'name'),
|
236
|
-
|
237
|
-
('age', 'age'),
|
238
|
-
|
239
|
-
),
|
240
|
-
|
241
|
-
field_labels={
|
242
|
-
|
243
|
-
'name': '名称',
|
244
|
-
|
245
|
-
'age': '文',
|
246
|
-
|
247
|
-
},
|
248
|
-
|
249
|
-
label='並び順'
|
250
|
-
|
251
|
-
)
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
def filter_age(self, queryset, name, value):
|
256
|
-
|
257
|
-
s = name
|
258
|
-
|
259
|
-
s_l = s.split('。')
|
260
|
-
|
261
|
-
word = value
|
262
|
-
|
263
|
-
for i in s_l:
|
264
|
-
|
265
|
-
if word in i:
|
266
|
-
|
267
|
-
return queryset.filter(i)
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
class Meta:
|
272
|
-
|
273
|
-
model = Item
|
274
|
-
|
275
|
-
fields = ['name' ,'age',]
|
276
|
-
|
277
|
-
```
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
```HTML
|
282
|
-
|
283
|
-
#item_filter.html
|
284
|
-
|
285
|
-
<div class="row">
|
286
|
-
|
287
|
-
<div class="col-12">
|
288
|
-
|
289
|
-
<ul class="list-group">
|
290
|
-
|
291
|
-
{% for item in item_list %}
|
292
|
-
|
293
|
-
<li class="list-group-item">
|
294
|
-
|
295
|
-
<div class="row">
|
296
|
-
|
297
|
-
<div class="col-3">
|
298
|
-
|
299
|
-
<p>名称</p>
|
300
|
-
|
301
|
-
</div>
|
302
|
-
|
303
|
-
<div class="col-9">
|
304
|
-
|
305
|
-
<p>{{ item.name }}</p>
|
306
|
-
|
307
|
-
</div>
|
308
|
-
|
309
|
-
</div>
|
310
|
-
|
311
|
-
<div class="row">
|
312
|
-
|
313
|
-
<div class="col-3">
|
314
|
-
|
315
|
-
<p>文</p>
|
316
|
-
|
317
|
-
</div>
|
318
|
-
|
319
|
-
<div class="col-9">
|
320
|
-
|
321
|
-
<p>{{ item.age }}</p> <!-- ここでmy_functionを呼ぶ -->
|
322
|
-
|
323
|
-
</div>
|
324
|
-
|
325
|
-
</div>
|
326
|
-
|
327
|
-
```
|
3
検討過程の追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -4,77 +4,267 @@
|
|
4
4
|
|
5
5
|
|
6
6
|
|
7
|
+
下記のサイトを参考にして、解決方法を模索しました。
|
8
|
+
|
9
|
+
・views.pyのTemplateViewを使うこと
|
10
|
+
|
11
|
+
[Django でまず覚えたい TemplateView のパターン](https://qiita.com/ytyng/items/7cb3c3a5605974151678)
|
12
|
+
|
13
|
+
・Methodにより、クエリセットの処理方法をフィルターに指示する
|
14
|
+
|
15
|
+
[Django フィルターリファレンス](https://django-filter.readthedocs.io/en/latest/ref/filters.html?highlight=method#method)
|
16
|
+
|
17
|
+
・Methodによる検索条件の設定方法
|
18
|
+
|
19
|
+
[django-filterで検索用パラメータを受け取り加工してから検索する方法](https://teratail.com/questions/131200)
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
たどり着いたのが、filter.pyで、Methodによりfilterに関数を付与する方法です。
|
24
|
+
|
25
|
+
しかし、実行すると「AssertionError at /
|
26
|
+
|
27
|
+
Expected 'ItemFilter.age' to return a QuerySet, but got a NoneType instead.」
|
28
|
+
|
29
|
+
というエラーメッセージが出てしまい、先に進むことができなくなってしまいました・・・。
|
30
|
+
|
31
|
+
|
32
|
+
|
7
33
|
```python
|
8
34
|
|
35
|
+
from django import forms
|
36
|
+
|
37
|
+
from .models import Item
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
class ItemForm(forms.ModelForm):
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class Meta:
|
46
|
+
|
9
|
-
|
47
|
+
model = Item
|
10
|
-
|
11
|
-
|
48
|
+
|
12
|
-
|
13
|
-
s
|
49
|
+
fields = ('name','age')
|
14
|
-
|
15
|
-
|
50
|
+
|
16
|
-
|
17
|
-
for i in s_l:
|
18
|
-
|
19
|
-
|
51
|
+
widgets = {
|
20
|
-
|
21
|
-
|
52
|
+
|
22
|
-
|
23
|
-
```
|
24
|
-
|
25
|
-
```HTML
|
26
|
-
|
27
|
-
#item_form.html フォームのテンプレート
|
28
|
-
|
29
|
-
|
53
|
+
'name': forms.TextInput(attrs={'placeholder':''}),
|
30
|
-
|
31
|
-
|
54
|
+
|
32
|
-
|
33
|
-
{% block content %}
|
34
|
-
|
35
|
-
{{ form.certifications.errors }}
|
36
|
-
|
37
|
-
<div class="container">
|
38
|
-
|
39
|
-
<div class="row">
|
40
|
-
|
41
|
-
<div class="col-12">
|
42
|
-
|
43
|
-
|
55
|
+
'age': forms.Textarea(attrs={'rows':4}),
|
44
|
-
|
45
|
-
|
56
|
+
|
46
|
-
|
47
|
-
</div>
|
48
|
-
|
49
|
-
<div class="row">
|
50
|
-
|
51
|
-
<div class="col-12">
|
52
|
-
|
53
|
-
<form method="post" id="myform">
|
54
|
-
|
55
|
-
{%crispy form%}
|
56
|
-
|
57
|
-
</form>
|
58
|
-
|
59
|
-
</div>
|
60
|
-
|
61
|
-
</div>
|
62
|
-
|
63
|
-
</div>
|
64
|
-
|
65
|
-
|
57
|
+
}
|
66
58
|
|
67
59
|
```
|
68
60
|
|
69
61
|
```python
|
70
62
|
|
63
|
+
#models.py
|
64
|
+
|
71
|
-
from django import
|
65
|
+
from django.db import models
|
66
|
+
|
67
|
+
class Item(models.Model):
|
68
|
+
|
69
|
+
name = models.CharField(
|
70
|
+
|
71
|
+
verbose_name='name',
|
72
|
+
|
73
|
+
max_length=200,
|
74
|
+
|
75
|
+
)
|
76
|
+
|
77
|
+
age = models.TextField(
|
78
|
+
|
79
|
+
verbose_name='age',
|
80
|
+
|
81
|
+
max_length=300,
|
82
|
+
|
83
|
+
blank=True,
|
84
|
+
|
85
|
+
null=True,
|
86
|
+
|
87
|
+
)
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def my_function(self):
|
92
|
+
|
93
|
+
s = 'ここに、データベースの値をいれる。句読点で、文を区切る。'
|
94
|
+
|
95
|
+
s_l = s.split('。')
|
96
|
+
|
97
|
+
if request.method == "GET":
|
98
|
+
|
99
|
+
form = ItemForm(request.GET)
|
100
|
+
|
101
|
+
if form.is_valid():
|
102
|
+
|
103
|
+
d = form.cleaned_data['rows']
|
104
|
+
|
105
|
+
for i in s_l:
|
106
|
+
|
107
|
+
if d in i:
|
108
|
+
|
109
|
+
return render(request,'',{'rows':d,},)
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
# 管理サイト上の表示設定
|
114
|
+
|
115
|
+
def __str__(self):
|
116
|
+
|
117
|
+
return self.name
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
```python
|
122
|
+
|
123
|
+
#views.py
|
124
|
+
|
125
|
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
126
|
+
|
127
|
+
from django.urls import reverse_lazy
|
128
|
+
|
129
|
+
from django.views.generic import DetailView
|
130
|
+
|
131
|
+
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
132
|
+
|
133
|
+
from django_filters.views import FilterView
|
134
|
+
|
135
|
+
from .filters import ItemFilter
|
136
|
+
|
137
|
+
from .forms import ItemForm
|
72
138
|
|
73
139
|
from .models import Item
|
74
140
|
|
75
141
|
|
76
142
|
|
143
|
+
# Create your views here.
|
144
|
+
|
145
|
+
# 検索一覧画面
|
146
|
+
|
147
|
+
class ItemFilterView(LoginRequiredMixin, FilterView):
|
148
|
+
|
149
|
+
model = Item
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
# django-filter用設定
|
154
|
+
|
155
|
+
filterset_class = ItemFilter
|
156
|
+
|
157
|
+
strict = False
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# 1ページあたりの表示件数
|
162
|
+
|
163
|
+
paginate_by = 10
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
# 検索条件をセッションに保存する
|
168
|
+
|
169
|
+
def get(self, request, **kwargs):
|
170
|
+
|
171
|
+
if request.GET:
|
172
|
+
|
173
|
+
request.session['query'] = request.GET
|
174
|
+
|
175
|
+
else:
|
176
|
+
|
177
|
+
request.GET = request.GET.copy()
|
178
|
+
|
179
|
+
if 'query' in request.session.keys():
|
180
|
+
|
181
|
+
for key in request.session['query'].keys():
|
182
|
+
|
183
|
+
request.GET[key] = request.session['query'][key]
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
return super().get(request, **kwargs)
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
# 詳細画面
|
192
|
+
|
193
|
+
class ItemDetailView(LoginRequiredMixin, DetailView):
|
194
|
+
|
195
|
+
model = Item
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
```python
|
200
|
+
|
201
|
+
#filter.py
|
202
|
+
|
203
|
+
from django_filters import FilterSet
|
204
|
+
|
205
|
+
from django_filters import filters
|
206
|
+
|
207
|
+
from .models import Item
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
class MyOrderingFilter(filters.OrderingFilter):
|
212
|
+
|
213
|
+
descending_fmt = '%s (降順)'
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
|
218
|
+
|
77
|
-
class ItemF
|
219
|
+
class ItemFilter(FilterSet):
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
name = filters.CharFilter(label='名称', lookup_expr='contains')
|
224
|
+
|
225
|
+
age = filters.CharFilter(method='filter_age')
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
order_by = MyOrderingFilter(
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
fields=(
|
234
|
+
|
235
|
+
('name', 'name'),
|
236
|
+
|
237
|
+
('age', 'age'),
|
238
|
+
|
239
|
+
),
|
240
|
+
|
241
|
+
field_labels={
|
242
|
+
|
243
|
+
'name': '名称',
|
244
|
+
|
245
|
+
'age': '文',
|
246
|
+
|
247
|
+
},
|
248
|
+
|
249
|
+
label='並び順'
|
250
|
+
|
251
|
+
)
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def filter_age(self, queryset, name, value):
|
256
|
+
|
257
|
+
s = name
|
258
|
+
|
259
|
+
s_l = s.split('。')
|
260
|
+
|
261
|
+
word = value
|
262
|
+
|
263
|
+
for i in s_l:
|
264
|
+
|
265
|
+
if word in i:
|
266
|
+
|
267
|
+
return queryset.filter(i)
|
78
268
|
|
79
269
|
|
80
270
|
|
@@ -82,232 +272,6 @@
|
|
82
272
|
|
83
273
|
model = Item
|
84
274
|
|
85
|
-
fields = ('name','age')
|
86
|
-
|
87
|
-
widgets = {
|
88
|
-
|
89
|
-
'name': forms.TextInput(attrs={'placeholder':''}),
|
90
|
-
|
91
|
-
'age': forms.Textarea(attrs={'rows':4}),
|
92
|
-
|
93
|
-
}
|
94
|
-
|
95
|
-
```
|
96
|
-
|
97
|
-
```python
|
98
|
-
|
99
|
-
#models.py
|
100
|
-
|
101
|
-
from django.db import models
|
102
|
-
|
103
|
-
class Item(models.Model):
|
104
|
-
|
105
|
-
name = models.CharField(
|
106
|
-
|
107
|
-
verbose_name='name',
|
108
|
-
|
109
|
-
max_length=200,
|
110
|
-
|
111
|
-
)
|
112
|
-
|
113
|
-
age = models.TextField(
|
114
|
-
|
115
|
-
verbose_name='age',
|
116
|
-
|
117
|
-
max_length=300,
|
118
|
-
|
119
|
-
blank=True,
|
120
|
-
|
121
|
-
null=True,
|
122
|
-
|
123
|
-
)
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
def my_function(self):
|
128
|
-
|
129
|
-
s = 'ここに、データベースの値をいれる。句読点で、文を区切る。'
|
130
|
-
|
131
|
-
s_l = s.split('。')
|
132
|
-
|
133
|
-
if request.method == "GET":
|
134
|
-
|
135
|
-
form = ItemForm(request.GET)
|
136
|
-
|
137
|
-
if form.is_valid():
|
138
|
-
|
139
|
-
d = form.cleaned_data['rows']
|
140
|
-
|
141
|
-
for i in s_l:
|
142
|
-
|
143
|
-
if d in i:
|
144
|
-
|
145
|
-
return render(request,'',{'rows':d,},)
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
# 管理サイト上の表示設定
|
150
|
-
|
151
|
-
def __str__(self):
|
152
|
-
|
153
|
-
return self.name
|
154
|
-
|
155
|
-
```
|
156
|
-
|
157
|
-
```python
|
158
|
-
|
159
|
-
#views.py
|
160
|
-
|
161
|
-
from django.contrib.auth.mixins import LoginRequiredMixin
|
162
|
-
|
163
|
-
from django.urls import reverse_lazy
|
164
|
-
|
165
|
-
from django.views.generic import DetailView
|
166
|
-
|
167
|
-
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
168
|
-
|
169
|
-
from django_filters.views import FilterView
|
170
|
-
|
171
|
-
from .filters import ItemFilter
|
172
|
-
|
173
|
-
from .forms import ItemForm
|
174
|
-
|
175
|
-
from .models import Item
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
# Create your views here.
|
180
|
-
|
181
|
-
# 検索一覧画面
|
182
|
-
|
183
|
-
class ItemFilterView(LoginRequiredMixin, FilterView):
|
184
|
-
|
185
|
-
model = Item
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
# django-filter用設定
|
190
|
-
|
191
|
-
filterset_class = ItemFilter
|
192
|
-
|
193
|
-
strict = False
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
# 1ページあたりの表示件数
|
198
|
-
|
199
|
-
paginate_by = 10
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
# 検索条件をセッションに保存する
|
204
|
-
|
205
|
-
def get(self, request, **kwargs):
|
206
|
-
|
207
|
-
if request.GET:
|
208
|
-
|
209
|
-
request.session['query'] = request.GET
|
210
|
-
|
211
|
-
else:
|
212
|
-
|
213
|
-
request.GET = request.GET.copy()
|
214
|
-
|
215
|
-
if 'query' in request.session.keys():
|
216
|
-
|
217
|
-
for key in request.session['query'].keys():
|
218
|
-
|
219
|
-
request.GET[key] = request.session['query'][key]
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
return super().get(request, **kwargs)
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
# 詳細画面
|
228
|
-
|
229
|
-
class ItemDetailView(LoginRequiredMixin, DetailView):
|
230
|
-
|
231
|
-
model = Item
|
232
|
-
|
233
|
-
```
|
234
|
-
|
235
|
-
```python
|
236
|
-
|
237
|
-
#filter.py
|
238
|
-
|
239
|
-
from django_filters import FilterSet
|
240
|
-
|
241
|
-
from django_filters import filters
|
242
|
-
|
243
|
-
from .models import Item
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
class MyOrderingFilter(filters.OrderingFilter):
|
248
|
-
|
249
|
-
descending_fmt = '%s (降順)'
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
class ItemFilter(FilterSet):
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
name = filters.CharFilter(label='名称', lookup_expr='contains')
|
260
|
-
|
261
|
-
age = filters.CharFilter(method='filter_age')
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
order_by = MyOrderingFilter(
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
fields=(
|
270
|
-
|
271
|
-
('name', 'name'),
|
272
|
-
|
273
|
-
('age', 'age'),
|
274
|
-
|
275
|
-
),
|
276
|
-
|
277
|
-
field_labels={
|
278
|
-
|
279
|
-
'name': '名称',
|
280
|
-
|
281
|
-
'age': '文',
|
282
|
-
|
283
|
-
},
|
284
|
-
|
285
|
-
label='並び順'
|
286
|
-
|
287
|
-
)
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
def filter_first_age(self, queryset, name, value):
|
292
|
-
|
293
|
-
s = name
|
294
|
-
|
295
|
-
s_l = s.split('。')
|
296
|
-
|
297
|
-
word = value
|
298
|
-
|
299
|
-
for i in s_l:
|
300
|
-
|
301
|
-
if word in i:
|
302
|
-
|
303
|
-
return queryset.filter(i)
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
class Meta:
|
308
|
-
|
309
|
-
model = Item
|
310
|
-
|
311
275
|
fields = ['name' ,'age',]
|
312
276
|
|
313
277
|
```
|
2
filterの追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -224,27 +224,95 @@
|
|
224
224
|
|
225
225
|
|
226
226
|
|
227
|
+
# 詳細画面
|
228
|
+
|
229
|
+
class ItemDetailView(LoginRequiredMixin, DetailView):
|
230
|
+
|
231
|
+
model = Item
|
232
|
+
|
233
|
+
```
|
234
|
+
|
235
|
+
```python
|
236
|
+
|
237
|
+
#filter.py
|
238
|
+
|
239
|
+
from django_filters import FilterSet
|
240
|
+
|
241
|
+
from django_filters import filters
|
242
|
+
|
243
|
+
from .models import Item
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
class MyOrderingFilter(filters.OrderingFilter):
|
248
|
+
|
249
|
+
descending_fmt = '%s (降順)'
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
|
254
|
+
|
227
|
-
|
255
|
+
class ItemFilter(FilterSet):
|
256
|
+
|
257
|
+
|
258
|
+
|
228
|
-
|
259
|
+
name = filters.CharFilter(label='名称', lookup_expr='contains')
|
260
|
+
|
261
|
+
age = filters.CharFilter(method='filter_age')
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
order_by = MyOrderingFilter(
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
fields=(
|
270
|
+
|
229
|
-
|
271
|
+
('name', 'name'),
|
272
|
+
|
273
|
+
('age', 'age'),
|
274
|
+
|
275
|
+
),
|
276
|
+
|
277
|
+
field_labels={
|
278
|
+
|
279
|
+
'name': '名称',
|
280
|
+
|
281
|
+
'age': '文',
|
282
|
+
|
283
|
+
},
|
284
|
+
|
285
|
+
label='並び順'
|
286
|
+
|
287
|
+
)
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
def filter_first_age(self, queryset, name, value):
|
292
|
+
|
293
|
+
s = name
|
230
294
|
|
231
295
|
s_l = s.split('。')
|
232
296
|
|
233
|
-
word =
|
297
|
+
word = value
|
234
298
|
|
235
299
|
for i in s_l:
|
236
300
|
|
237
301
|
if word in i:
|
238
302
|
|
239
|
-
return i
|
303
|
+
return queryset.filter(i)
|
240
|
-
|
304
|
+
|
305
|
+
|
306
|
+
|
241
|
-
|
307
|
+
class Meta:
|
242
|
-
|
243
|
-
|
308
|
+
|
244
|
-
|
245
|
-
model = Item
|
309
|
+
model = Item
|
310
|
+
|
246
|
-
|
311
|
+
fields = ['name' ,'age',]
|
312
|
+
|
247
|
-
```
|
313
|
+
```
|
314
|
+
|
315
|
+
|
248
316
|
|
249
317
|
```HTML
|
250
318
|
|
@@ -276,7 +344,7 @@
|
|
276
344
|
|
277
345
|
</div>
|
278
346
|
|
279
|
-
<div class="row">
|
347
|
+
<div class="row">
|
280
348
|
|
281
349
|
<div class="col-3">
|
282
350
|
|
@@ -286,7 +354,7 @@
|
|
286
354
|
|
287
355
|
<div class="col-9">
|
288
356
|
|
289
|
-
<p>{{ item.
|
357
|
+
<p>{{ item.age }}</p> <!-- ここでmy_functionを呼ぶ -->
|
290
358
|
|
291
359
|
</div>
|
292
360
|
|
1
views.pyを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -130,6 +130,106 @@
|
|
130
130
|
|
131
131
|
s_l = s.split('。')
|
132
132
|
|
133
|
+
if request.method == "GET":
|
134
|
+
|
135
|
+
form = ItemForm(request.GET)
|
136
|
+
|
137
|
+
if form.is_valid():
|
138
|
+
|
139
|
+
d = form.cleaned_data['rows']
|
140
|
+
|
141
|
+
for i in s_l:
|
142
|
+
|
143
|
+
if d in i:
|
144
|
+
|
145
|
+
return render(request,'',{'rows':d,},)
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
# 管理サイト上の表示設定
|
150
|
+
|
151
|
+
def __str__(self):
|
152
|
+
|
153
|
+
return self.name
|
154
|
+
|
155
|
+
```
|
156
|
+
|
157
|
+
```python
|
158
|
+
|
159
|
+
#views.py
|
160
|
+
|
161
|
+
from django.contrib.auth.mixins import LoginRequiredMixin
|
162
|
+
|
163
|
+
from django.urls import reverse_lazy
|
164
|
+
|
165
|
+
from django.views.generic import DetailView
|
166
|
+
|
167
|
+
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
168
|
+
|
169
|
+
from django_filters.views import FilterView
|
170
|
+
|
171
|
+
from .filters import ItemFilter
|
172
|
+
|
173
|
+
from .forms import ItemForm
|
174
|
+
|
175
|
+
from .models import Item
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
# Create your views here.
|
180
|
+
|
181
|
+
# 検索一覧画面
|
182
|
+
|
183
|
+
class ItemFilterView(LoginRequiredMixin, FilterView):
|
184
|
+
|
185
|
+
model = Item
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
# django-filter用設定
|
190
|
+
|
191
|
+
filterset_class = ItemFilter
|
192
|
+
|
193
|
+
strict = False
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
# 1ページあたりの表示件数
|
198
|
+
|
199
|
+
paginate_by = 10
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
# 検索条件をセッションに保存する
|
204
|
+
|
205
|
+
def get(self, request, **kwargs):
|
206
|
+
|
207
|
+
if request.GET:
|
208
|
+
|
209
|
+
request.session['query'] = request.GET
|
210
|
+
|
211
|
+
else:
|
212
|
+
|
213
|
+
request.GET = request.GET.copy()
|
214
|
+
|
215
|
+
if 'query' in request.session.keys():
|
216
|
+
|
217
|
+
for key in request.session['query'].keys():
|
218
|
+
|
219
|
+
request.GET[key] = request.session['query'][key]
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
return super().get(request, **kwargs)
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def my_function(self):
|
228
|
+
|
229
|
+
s = 'ここに、データベースの値をいれる。句読点で、文を区切る。'
|
230
|
+
|
231
|
+
s_l = s.split('。')
|
232
|
+
|
133
233
|
word = 'フォームで入力された値'
|
134
234
|
|
135
235
|
for i in s_l:
|
@@ -138,13 +238,11 @@
|
|
138
238
|
|
139
239
|
return i
|
140
240
|
|
141
|
-
|
142
|
-
|
143
|
-
|
241
|
+
# 詳細画面
|
242
|
+
|
144
|
-
|
243
|
+
class ItemDetailView(LoginRequiredMixin, DetailView):
|
244
|
+
|
145
|
-
de
|
245
|
+
model = Item
|
146
|
-
|
147
|
-
return self.name
|
148
246
|
|
149
247
|
```
|
150
248
|
|