質問編集履歴
3
最後のひと押しで、filterが反映されずに困っています。
test
CHANGED
File without changes
|
test
CHANGED
@@ -144,103 +144,7 @@
|
|
144
144
|
|
145
145
|
```html
|
146
146
|
|
147
|
-
{% extends 'base.html' %}
|
148
|
-
|
149
|
-
{% load static %}
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
{% block content %}
|
154
|
-
|
155
|
-
<div class="selectContentArea">
|
156
|
-
|
157
|
-
<div class="selectTxtArea">
|
158
|
-
|
159
|
-
<h2>どこへ、食べに行きますか?</h2>
|
160
|
-
|
161
|
-
</div>
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
<div class="selectAreaArea">
|
166
|
-
|
167
|
-
<div class="selectAreaBox">
|
168
|
-
|
169
|
-
<form method="POST">
|
170
|
-
|
171
|
-
{% csrf_token %}
|
172
|
-
|
173
|
-
{% for field in test_form %}
|
174
|
-
|
175
|
-
{{ field.label }}
|
176
|
-
|
177
|
-
{{ field }}
|
178
|
-
|
179
|
-
{% endfor%}
|
180
|
-
|
181
|
-
<input type="submit" id="button" value="GO">
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
</div>
|
186
|
-
|
187
|
-
</div>
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
<div class="selectDecideButtonArea">
|
192
|
-
|
193
|
-
<div class="selectDecideButton">
|
194
|
-
|
195
|
-
<p>Go!</p>
|
196
|
-
|
197
|
-
</div>
|
198
|
-
|
199
|
-
</div>
|
200
|
-
|
201
|
-
</div>
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
<section class="post-list">
|
206
|
-
|
207
|
-
{% if object_list|length == 0 %}
|
208
|
-
|
209
|
-
<p>検索結果が存在しません。</p>
|
210
|
-
|
211
|
-
{% else %}
|
212
|
-
|
213
|
-
<table class="table table-hover table-bordered">
|
214
|
-
|
215
|
-
<tr>
|
216
|
-
|
217
|
-
|
147
|
+
#selectArea.html ※更新につき、削除しました。
|
218
|
-
|
219
|
-
</tr>
|
220
|
-
|
221
|
-
<tbody>
|
222
|
-
|
223
|
-
{% for post in object_list %}
|
224
|
-
|
225
|
-
<tr>
|
226
|
-
|
227
|
-
<td width="35%">{{ post.area }}</td>
|
228
|
-
|
229
|
-
</tr>
|
230
|
-
|
231
|
-
{% endfor %}
|
232
|
-
|
233
|
-
</tbody>
|
234
|
-
|
235
|
-
</table>
|
236
|
-
|
237
|
-
{% endif %}
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
{% endblock content %}
|
244
148
|
|
245
149
|
```
|
246
150
|
|
@@ -280,7 +184,7 @@
|
|
280
184
|
|
281
185
|
|
282
186
|
|
283
|
-
|
187
|
+
### 以下、2019/10/3追記**
|
284
188
|
|
285
189
|
views.pyにloggingモジュールを仕込んで、挙動を見てみました。
|
286
190
|
|
@@ -332,7 +236,7 @@
|
|
332
236
|
|
333
237
|
|
334
238
|
|
335
|
-
|
239
|
+
### 以下、2019/10/3 夕方追記
|
336
240
|
|
337
241
|
@t_obaraさん、引き続きありがとうございます!
|
338
242
|
|
@@ -376,6 +280,98 @@
|
|
376
280
|
|
377
281
|
```python
|
378
282
|
|
283
|
+
#views.py ※更新につき、削除しました。
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
```
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
エラー文は、以下のとおりです。
|
292
|
+
|
293
|
+
```console
|
294
|
+
|
295
|
+
Related Field got invalid lookup: icontains
|
296
|
+
|
297
|
+
```
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
明確に該当部分のエラーだとはわかりつつも、ググって出てくる情報に、
|
302
|
+
|
303
|
+
日本語のものがいつも以上に少なく、理解に苦しんでいます。。。
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
まずは、経過だけ書かせていただき、一休みしたらもうワントライしてみます。
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
### 以下、2019/10/6 深夜追記
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
半泣きになりながら調べては試し、失敗してはまた調べを続けて、ようやっとゴールに近づいてきた感じがします......!
|
316
|
+
|
317
|
+
![イメージ説明](cb0f8f2f62d26ac6877cac50ffbfa2b4.png)
|
318
|
+
|
319
|
+
「プルダウンメニューをつくる」「postメソッドで値を返す」そこまではできているのです。
|
320
|
+
|
321
|
+
あとは、参照した値に応じてフィルターをかけるだけなのですが、そこでエラーが起きてしまいます......
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
```python
|
326
|
+
|
327
|
+
#forms.py
|
328
|
+
|
329
|
+
from django import forms
|
330
|
+
|
331
|
+
from .models import restaurantDetail, restaurantArea, restaurantGenre
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
areaChoices = [
|
336
|
+
|
337
|
+
(1, '中目黒'),
|
338
|
+
|
339
|
+
(2, '渋谷'),
|
340
|
+
|
341
|
+
(3, '新宿'),
|
342
|
+
|
343
|
+
(4, '恵比寿'),
|
344
|
+
|
345
|
+
(5, '品川'),
|
346
|
+
|
347
|
+
]
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
class selectForm(forms.Form):
|
352
|
+
|
353
|
+
area = forms.ChoiceField(
|
354
|
+
|
355
|
+
label="エリア",
|
356
|
+
|
357
|
+
required=True,
|
358
|
+
|
359
|
+
disabled=False,
|
360
|
+
|
361
|
+
initial=['1'],
|
362
|
+
|
363
|
+
choices=areaChoices,
|
364
|
+
|
365
|
+
)
|
366
|
+
|
367
|
+
```
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
```python
|
372
|
+
|
373
|
+
#views.py
|
374
|
+
|
379
375
|
class selectView(ListView):
|
380
376
|
|
381
377
|
model = restaurantDetail
|
@@ -384,148 +380,96 @@
|
|
384
380
|
|
385
381
|
|
386
382
|
|
387
|
-
def
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
f
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
request.
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
logger.info("form_value") #Logging
|
414
|
-
|
415
|
-
logger.info(form_value) #Logging
|
416
|
-
|
417
|
-
return self.get(request, *args, **kwargs)
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
def get_context_data(self, **kwargs):
|
422
|
-
|
423
|
-
logger.info('Test2') #Logging
|
424
|
-
|
425
|
-
context = super().get_context_data(**kwargs)
|
426
|
-
|
427
|
-
restaurantDetailArea = ''
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
if 'form_value' in self.request.session:
|
432
|
-
|
433
|
-
logger.info('session is alive!!') #Logging
|
434
|
-
|
435
|
-
form_value = self.request.session['form_value']
|
436
|
-
|
437
|
-
restaurantDetailArea = form_value[0]
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
default_data = {'restaurantDetailArea': restaurantDetailArea,}
|
442
|
-
|
443
|
-
test_form = searchForm(initial = default_data)
|
444
|
-
|
445
|
-
context['test_form'] = test_form
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
logger.info(test_form)#Logging
|
450
|
-
|
451
|
-
logger.info(context)#Logging
|
452
|
-
|
453
|
-
return context
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
def get_queryset(self):
|
458
|
-
|
459
|
-
logger.info('Test3') #Logging
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
if 'form_value' in self.request.session:
|
464
|
-
|
465
|
-
logger.info('session is alive!! again!!') #Logging
|
466
|
-
|
467
|
-
form_value = self.request.session['form_value']
|
468
|
-
|
469
|
-
restaurantDetailArea = form_value[0]
|
470
|
-
|
471
|
-
condition_restaurantDetailArea = Q()
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
logger.info(form_value) #Logging
|
476
|
-
|
477
|
-
logger.info(restaurantDetailArea)#Logging
|
478
|
-
|
479
|
-
if restaurantDetailArea != None:
|
480
|
-
|
481
|
-
if len(restaurantDetailArea) != 0 and restaurantDetailArea[0]:
|
482
|
-
|
483
|
-
condition_restaurantDetailArea = Q(restaurantDetailArea__icontains=restaurantDetailArea) #<-ここがエラー引き起こしています。
|
484
|
-
|
485
|
-
else:
|
486
|
-
|
487
|
-
pass
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
return restaurantDetail.objects.select_related().filter(condition_restaurantDetailArea)
|
492
|
-
|
493
|
-
|
383
|
+
def get(self, request):
|
384
|
+
|
385
|
+
f = {'form': selectForm(),}
|
386
|
+
|
387
|
+
return render(request, 'areaSelect.html', f)
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
def post(self, *args, **kwargs):
|
392
|
+
|
393
|
+
return self.get_queryset(self)
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
def get_queryset(self, request):
|
398
|
+
|
399
|
+
if self.request.method == 'POST':
|
400
|
+
|
401
|
+
form_selected = []
|
402
|
+
|
403
|
+
form_selected.append(self.request.POST.get('area'))
|
404
|
+
|
405
|
+
form_selected_num = form_selected[0]
|
406
|
+
|
407
|
+
return restaurantDetail.objects.filter(restaurantDetailArea=form_selected_num)
|
494
408
|
|
495
409
|
else:
|
496
410
|
|
411
|
+
d = {'form': selectForm(),}
|
412
|
+
|
497
|
-
return resta
|
413
|
+
return render(request, 'areaSelect.html', d)
|
498
|
-
|
414
|
+
|
499
|
-
```
|
415
|
+
```
|
500
|
-
|
501
|
-
|
502
|
-
|
416
|
+
|
417
|
+
|
418
|
+
|
503
|
-
|
419
|
+
上記コードを動かすと、
|
420
|
+
|
421
|
+
|
504
422
|
|
505
423
|
```console
|
506
424
|
|
507
|
-
|
425
|
+
too many values to unpack (expected 2)
|
508
|
-
|
426
|
+
|
509
|
-
```
|
427
|
+
```
|
510
|
-
|
511
|
-
|
512
|
-
|
428
|
+
|
513
|
-
|
429
|
+
というエラー文が返ってきてしまいます。
|
430
|
+
|
514
|
-
|
431
|
+
なお、上記からは削除しましたが、
|
432
|
+
|
433
|
+
loggingを随所に差し込んで、
|
434
|
+
|
435
|
+
form_selected_num = form_selected[0]
|
436
|
+
|
515
|
-
|
437
|
+
までは、値のリレーが思惑通りOKな様子も確認できています。
|
438
|
+
|
439
|
+
|
440
|
+
|
516
|
-
|
441
|
+
このエラー文について、他の方の投稿などを拝見するに、
|
442
|
+
|
517
|
-
```python
|
443
|
+
```python
|
518
|
-
|
444
|
+
|
519
|
-
|
445
|
+
#views.py
|
520
|
-
|
446
|
+
|
521
|
-
|
447
|
+
return restaurantDetail.objects.filter(restaurantDetailArea=form_selected_num)
|
522
|
-
|
523
|
-
|
448
|
+
|
524
|
-
|
525
|
-
else:
|
526
|
-
|
527
|
-
pass
|
528
|
-
|
529
|
-
```
|
449
|
+
```
|
450
|
+
|
530
|
-
|
451
|
+
ここの書き方で、引数(上記の場合"restaurantDetailArea")を書きそびれた結果起きているように見受けられます。
|
452
|
+
|
453
|
+
しかし、じぶんのコードの場合そこは満たしており、実際直前に別の形でこのfilterだけを動かしていた内は、問題なく稼働していました。
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
あまり見受けられない書き方でいうと
|
458
|
+
|
459
|
+
```python
|
460
|
+
|
461
|
+
#views.py
|
462
|
+
|
463
|
+
def post(self, *args, **kwargs):
|
464
|
+
|
465
|
+
return self.get_queryset(self)```
|
466
|
+
|
467
|
+
```
|
468
|
+
|
469
|
+
この部分が原因かなとも思いつつ、確証はありません。
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
ここまで来たら、なんとかしたいです!
|
474
|
+
|
531
|
-
|
475
|
+
ので、違和感ある箇所などあれば、ご指摘頂けますと幸いです......!!
|
2
TemplateView->ListViewにしたおかげで一歩進みました! が......、クエリで再度袋小路です。
test
CHANGED
File without changes
|
test
CHANGED
@@ -136,450 +136,396 @@
|
|
136
136
|
|
137
137
|
```Python
|
138
138
|
|
139
|
-
#views.py
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
class
|
139
|
+
#views.py ※更新につき、削除しました。
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
```html
|
146
|
+
|
147
|
+
{% extends 'base.html' %}
|
148
|
+
|
149
|
+
{% load static %}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
{% block content %}
|
154
|
+
|
155
|
+
<div class="selectContentArea">
|
156
|
+
|
157
|
+
<div class="selectTxtArea">
|
158
|
+
|
159
|
+
<h2>どこへ、食べに行きますか?</h2>
|
160
|
+
|
161
|
+
</div>
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
<div class="selectAreaArea">
|
166
|
+
|
167
|
+
<div class="selectAreaBox">
|
168
|
+
|
169
|
+
<form method="POST">
|
170
|
+
|
171
|
+
{% csrf_token %}
|
172
|
+
|
173
|
+
{% for field in test_form %}
|
174
|
+
|
175
|
+
{{ field.label }}
|
176
|
+
|
177
|
+
{{ field }}
|
178
|
+
|
179
|
+
{% endfor%}
|
180
|
+
|
181
|
+
<input type="submit" id="button" value="GO">
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
</div>
|
186
|
+
|
187
|
+
</div>
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
<div class="selectDecideButtonArea">
|
192
|
+
|
193
|
+
<div class="selectDecideButton">
|
194
|
+
|
195
|
+
<p>Go!</p>
|
196
|
+
|
197
|
+
</div>
|
198
|
+
|
199
|
+
</div>
|
200
|
+
|
201
|
+
</div>
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
<section class="post-list">
|
206
|
+
|
207
|
+
{% if object_list|length == 0 %}
|
208
|
+
|
209
|
+
<p>検索結果が存在しません。</p>
|
210
|
+
|
211
|
+
{% else %}
|
212
|
+
|
213
|
+
<table class="table table-hover table-bordered">
|
214
|
+
|
215
|
+
<tr>
|
216
|
+
|
217
|
+
<th>エリア</th>
|
218
|
+
|
219
|
+
</tr>
|
220
|
+
|
221
|
+
<tbody>
|
222
|
+
|
223
|
+
{% for post in object_list %}
|
224
|
+
|
225
|
+
<tr>
|
226
|
+
|
227
|
+
<td width="35%">{{ post.area }}</td>
|
228
|
+
|
229
|
+
</tr>
|
230
|
+
|
231
|
+
{% endfor %}
|
232
|
+
|
233
|
+
</tbody>
|
234
|
+
|
235
|
+
</table>
|
236
|
+
|
237
|
+
{% endif %}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
|
242
|
+
|
243
|
+
{% endblock content %}
|
244
|
+
|
245
|
+
```
|
246
|
+
|
247
|
+
restaurantDetailのデータベース仮データ中身
|
248
|
+
|
249
|
+
![restaurantDetailのデータベース仮データ中身](900961401684569adaf989db3f807e3b.png)
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
restaurantAreaのデータベース仮データ中身
|
254
|
+
|
255
|
+
上図のidは、こちらのAreaリストと対応して引っ張っていくようになっています。
|
256
|
+
|
257
|
+
![restaurantAreaのデータベース仮データ中身](82f633080ddfe6d1bf8d9820e199dad3.png)
|
258
|
+
|
259
|
+
「ここのリンクミスかな?」と検索窓に"2"とか"5"とか入れてみましたが、特に効果ありませんでした。
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
なお、開発環境は以下の通りです。
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
言語:python3.7.4
|
270
|
+
|
271
|
+
フレームワーク:django 2.0.6
|
272
|
+
|
273
|
+
ブラウザ:Google Chrome 77
|
274
|
+
|
275
|
+
エディタ:visual studio code 1.38
|
276
|
+
|
277
|
+
OS:Windows10 home
|
278
|
+
|
279
|
+
```
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
**以下、2019/10/3追記**
|
284
|
+
|
285
|
+
views.pyにloggingモジュールを仕込んで、挙動を見てみました。
|
286
|
+
|
287
|
+
それでわかったのは、def文の1個目2個目は問題なく出力してくれており、
|
288
|
+
|
289
|
+
変数も差し支えなく渡されている模様でした。
|
290
|
+
|
291
|
+
|
292
|
+
|
293
|
+
しかし、def文の3個目は、どうにも動いてくれすらしていないようでした。
|
294
|
+
|
295
|
+
具体的には、以下のようにLoggingを仕込んだ結果、'Test3'と書かれたものが出力されませんでした。
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
```Python
|
300
|
+
|
301
|
+
#views.py ※更新につき、削除しました。
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
```
|
306
|
+
|
307
|
+
ちなみに、仮にフォームに「渋谷」と書いた場合の出力は、
|
308
|
+
|
309
|
+
それぞれ以下のように出力されており、ちゃんと受け渡されている模様です。
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
```console
|
314
|
+
|
315
|
+
logger.info(form_value) -> ['渋谷']
|
316
|
+
|
317
|
+
logger.info(default_data) -> {'area': '渋谷'}
|
318
|
+
|
319
|
+
logger.info(test_form) -> <tr><th><label for="id_area">エリア:</label></th><td><input type="text" name="area" value="渋谷" required id="id_area"></td></tr>
|
320
|
+
|
321
|
+
logger.info(q_context) -> {'view': <restaurantsList.views.selectView object at 0x04734F50>, 'test_form': <searchForm bound=False, valid=False, fields=(area)>}
|
322
|
+
|
323
|
+
```
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
となると、def get_queryset(self):に該当する部分に問題があるということになるかと思うのですが、
|
328
|
+
|
329
|
+
それ以前に動きすらしていないので、原因がわからず......
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
|
334
|
+
|
335
|
+
**以下、2019/10/3 夕方追記**
|
336
|
+
|
337
|
+
@t_obaraさん、引き続きありがとうございます!
|
338
|
+
|
339
|
+
いただいたurl拝見しました。
|
340
|
+
|
341
|
+
[get、get_queryset、get_context_dataの違いについて。](https://teratail.com/questions/118626)
|
342
|
+
|
343
|
+
TemplateViewでは、get_querysetがを放ったらかしておくと動いてくれないものなのですね。
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
```python
|
348
|
+
|
349
|
+
class selectView(ListView):
|
350
|
+
|
351
|
+
```
|
352
|
+
|
353
|
+
に修正の上、以下を直しました。
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
(1)"area"という変数を新たにつくってしまっていたことによるエラー
|
358
|
+
|
359
|
+
->既存のmodel.pyに準じて、全て"restaurantDetailArea"に修正。
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
(2)len(restaurantDetailArea)のデフォルト値が、Noneであり、
|
364
|
+
|
365
|
+
'NoneType' has no len()エラーを起こす。
|
366
|
+
|
367
|
+
->if文で、Noneのときは、無視する条件文を追記することでクリア。
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
しかし、queryに関する部分のエラーは乗り越えられませんでした......
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
まず、現状のViews.py(該当のclassのみ)は以下の通りです。
|
376
|
+
|
377
|
+
```python
|
378
|
+
|
379
|
+
class selectView(ListView):
|
166
380
|
|
167
381
|
model = restaurantDetail
|
168
382
|
|
169
|
-
template_name = '
|
383
|
+
template_name = 'areaSelect.html'
|
384
|
+
|
385
|
+
|
386
|
+
|
387
|
+
def post(self, request, *args, **kwargs):
|
388
|
+
|
389
|
+
logger.info('Test1') #Logging
|
390
|
+
|
391
|
+
logger.info('first self')#Logging
|
392
|
+
|
393
|
+
logger.info(self) #Logging
|
394
|
+
|
395
|
+
|
396
|
+
|
397
|
+
form_value=[
|
398
|
+
|
399
|
+
self.request.POST.get('restaurantDetailArea', None),
|
400
|
+
|
401
|
+
]
|
402
|
+
|
403
|
+
request.session['form_value'] = form_value
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
#self.request.GET = self.request.GET.copy()
|
408
|
+
|
409
|
+
#self.request.GET.clear()
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
logger.info("form_value") #Logging
|
414
|
+
|
415
|
+
logger.info(form_value) #Logging
|
416
|
+
|
417
|
+
return self.get(request, *args, **kwargs)
|
170
418
|
|
171
419
|
|
172
420
|
|
173
421
|
def get_context_data(self, **kwargs):
|
174
422
|
|
423
|
+
logger.info('Test2') #Logging
|
424
|
+
|
175
425
|
context = super().get_context_data(**kwargs)
|
176
426
|
|
427
|
+
restaurantDetailArea = ''
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
if 'form_value' in self.request.session:
|
432
|
+
|
433
|
+
logger.info('session is alive!!') #Logging
|
434
|
+
|
435
|
+
form_value = self.request.session['form_value']
|
436
|
+
|
437
|
+
restaurantDetailArea = form_value[0]
|
438
|
+
|
439
|
+
|
440
|
+
|
177
|
-
|
441
|
+
default_data = {'restaurantDetailArea': restaurantDetailArea,}
|
442
|
+
|
178
|
-
|
443
|
+
test_form = searchForm(initial = default_data)
|
444
|
+
|
179
|
-
context['
|
445
|
+
context['test_form'] = test_form
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
logger.info(test_form)#Logging
|
450
|
+
|
451
|
+
logger.info(context)#Logging
|
180
452
|
|
181
453
|
return context
|
182
454
|
|
183
455
|
|
184
456
|
|
185
|
-
class restaurantDetailView(DetailView):
|
186
|
-
|
187
|
-
model = restaurantDetail
|
188
|
-
|
189
|
-
template_name = 'resutaurant_detail.html'
|
190
|
-
|
191
|
-
context_object_name = 'restaurantDetailObj'
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
def get_
|
457
|
+
def get_queryset(self):
|
196
|
-
|
197
|
-
|
458
|
+
|
198
|
-
|
199
|
-
context['restaurantAreaList'] = restaurantArea.objects.all()
|
200
|
-
|
201
|
-
context['restaurantGenreList'] = restaurantGenre.objects.all()
|
202
|
-
|
203
|
-
return context
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
class selectView(TemplateView):
|
208
|
-
|
209
|
-
model = restaurantDetail
|
210
|
-
|
211
|
-
template_name = 'areaSelect.html'
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
def post(self, request, *args, **kwargs):
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
form_value=[
|
220
|
-
|
221
|
-
|
459
|
+
logger.info('Test3') #Logging
|
222
|
-
|
223
|
-
]
|
224
|
-
|
225
|
-
request.session['form_value'] = form_value
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
self.request.GET = self.request.GET.copy()
|
230
|
-
|
231
|
-
self.request.GET.clear()
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
return self.get(request, *args, **kwargs)
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
def get_context_data(self, **kwargs):
|
240
|
-
|
241
|
-
context = super().get_context_data(**kwargs)
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
area = ''
|
246
460
|
|
247
461
|
|
248
462
|
|
249
463
|
if 'form_value' in self.request.session:
|
250
464
|
|
465
|
+
logger.info('session is alive!! again!!') #Logging
|
466
|
+
|
251
467
|
form_value = self.request.session['form_value']
|
252
468
|
|
253
|
-
area = form_value[0]
|
469
|
+
restaurantDetailArea = form_value[0]
|
470
|
+
|
471
|
+
condition_restaurantDetailArea = Q()
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
logger.info(form_value) #Logging
|
476
|
+
|
477
|
+
logger.info(restaurantDetailArea)#Logging
|
478
|
+
|
479
|
+
if restaurantDetailArea != None:
|
480
|
+
|
481
|
+
if len(restaurantDetailArea) != 0 and restaurantDetailArea[0]:
|
482
|
+
|
483
|
+
condition_restaurantDetailArea = Q(restaurantDetailArea__icontains=restaurantDetailArea) #<-ここがエラー引き起こしています。
|
484
|
+
|
485
|
+
else:
|
486
|
+
|
487
|
+
pass
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
return restaurantDetail.objects.select_related().filter(condition_restaurantDetailArea)
|
254
492
|
|
255
493
|
|
256
494
|
|
257
|
-
default_data = {'area': area,}
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
test_form = searchForm(initial=default_data)
|
262
|
-
|
263
|
-
context['test_form'] = test_form
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
return context
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
def get_queryset(self):
|
272
|
-
|
273
|
-
if 'form_value' in self.request.session:
|
274
|
-
|
275
|
-
form_value = self.request.session['form_value']
|
276
|
-
|
277
|
-
area = form_value[0]
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
conditionn_area = Q()
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
if len(area) != 0 and area[0]:
|
286
|
-
|
287
|
-
condition_area = Q(area__icontains=area)
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
return restaurantDetail.objects.select_related().filter(condition_area)
|
292
|
-
|
293
|
-
|
294
|
-
|
295
495
|
else:
|
296
496
|
|
297
497
|
return restaurantDetail.objects.none()
|
298
498
|
|
299
499
|
```
|
300
500
|
|
501
|
+
|
502
|
+
|
503
|
+
エラー文は、以下のとおりです。
|
504
|
+
|
301
|
-
```
|
505
|
+
```console
|
302
|
-
|
303
|
-
|
506
|
+
|
304
|
-
|
305
|
-
{% load static %}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
{% block content %}
|
310
|
-
|
311
|
-
<div class="selectContentArea">
|
312
|
-
|
313
|
-
|
507
|
+
Related Field got invalid lookup: icontains
|
314
|
-
|
315
|
-
|
508
|
+
|
316
|
-
|
317
|
-
</div>
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
<div class="selectAreaArea">
|
322
|
-
|
323
|
-
<div class="selectAreaBox">
|
324
|
-
|
325
|
-
<form method="POST">
|
326
|
-
|
327
|
-
{% csrf_token %}
|
328
|
-
|
329
|
-
{% for field in test_form %}
|
330
|
-
|
331
|
-
{{ field.label }}
|
332
|
-
|
333
|
-
{{ field }}
|
334
|
-
|
335
|
-
{% endfor%}
|
336
|
-
|
337
|
-
<input type="submit" id="button" value="GO">
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
</div>
|
342
|
-
|
343
|
-
</div>
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
<div class="selectDecideButtonArea">
|
348
|
-
|
349
|
-
<div class="selectDecideButton">
|
350
|
-
|
351
|
-
<p>Go!</p>
|
352
|
-
|
353
|
-
</div>
|
354
|
-
|
355
|
-
</div>
|
356
|
-
|
357
|
-
</div>
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
<section class="post-list">
|
362
|
-
|
363
|
-
{% if object_list|length == 0 %}
|
364
|
-
|
365
|
-
<p>検索結果が存在しません。</p>
|
366
|
-
|
367
|
-
{% else %}
|
368
|
-
|
369
|
-
<table class="table table-hover table-bordered">
|
370
|
-
|
371
|
-
<tr>
|
372
|
-
|
373
|
-
<th>エリア</th>
|
374
|
-
|
375
|
-
</tr>
|
376
|
-
|
377
|
-
<tbody>
|
378
|
-
|
379
|
-
{% for post in object_list %}
|
380
|
-
|
381
|
-
<tr>
|
382
|
-
|
383
|
-
<td width="35%">{{ post.area }}</td>
|
384
|
-
|
385
|
-
</tr>
|
386
|
-
|
387
|
-
{% endfor %}
|
388
|
-
|
389
|
-
</tbody>
|
390
|
-
|
391
|
-
</table>
|
392
|
-
|
393
|
-
{% endif %}
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
{% endblock content %}
|
400
|
-
|
401
|
-
```
|
509
|
+
```
|
402
|
-
|
403
|
-
|
510
|
+
|
404
|
-
|
405
|
-
|
511
|
+
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
512
|
+
|
410
|
-
|
411
|
-
上図のidは、こちらのAreaリストと対応して引っ張っていくようになっています。
|
412
|
-
|
413
|
-
![restaurantAreaのデータベース仮データ中身](82f633080ddfe6d1bf8d9820e199dad3.png)
|
414
|
-
|
415
|
-
「ここのリンクミスかな?」と検索窓に"2"とか"5"とか入れてみましたが、特に効果ありませんでした。
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
なお、開発環境は以下の通りです。
|
422
|
-
|
423
|
-
```
|
424
|
-
|
425
|
-
言語:python3.7.4
|
426
|
-
|
427
|
-
フレームワーク:django 2.0.6
|
428
|
-
|
429
|
-
ブラウザ:Google Chrome 77
|
430
|
-
|
431
|
-
エディタ:visual studio code 1.38
|
432
|
-
|
433
|
-
OS:Windows10 home
|
434
|
-
|
435
|
-
```
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
**以下、2019/10/3追記**
|
440
|
-
|
441
|
-
views.pyにloggingモジュールを仕込んで、挙動を見てみました。
|
442
|
-
|
443
|
-
|
513
|
+
明確に以下部分のエラーだとはわかりつつも、ググって出てくる情報に、
|
444
|
-
|
445
|
-
|
514
|
+
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
515
|
+
日本語のものがいつも以上に少なく、理解に苦しんでいます。。。
|
450
|
-
|
451
|
-
具体的には、以下のようにLoggingを仕込んだ結果、'Test3'と書かれたものが出力されませんでした。
|
452
|
-
|
453
|
-
|
454
516
|
|
455
517
|
```python
|
456
518
|
|
457
|
-
class selectView(TemplateView):
|
458
|
-
|
459
|
-
logger.info('Test1') #Logging追記
|
460
|
-
|
461
|
-
|
519
|
+
if restaurantDetailArea != None:
|
462
|
-
|
463
|
-
|
520
|
+
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
def post(self, request, *args, **kwargs):
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
form_value=[
|
472
|
-
|
473
|
-
self.request.POST.get('area', None),
|
474
|
-
|
475
|
-
]
|
476
|
-
|
477
|
-
request.session['form_value'] = form_value
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
self.request.GET = self.request.GET.copy()
|
482
|
-
|
483
|
-
self.request.GET.clear()
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
logger.info(form_value) #Logging追記
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
return self.get(request, *args, **kwargs)
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
def get_context_data(self, **kwargs):
|
496
|
-
|
497
|
-
logger.info('Test2') #Logging追記
|
498
|
-
|
499
|
-
q_context = super().get_context_data(**kwargs)
|
500
|
-
|
501
|
-
area = ''
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
if 'form_value' in self.request.session:
|
506
|
-
|
507
|
-
form_value = self.request.session['form_value']
|
508
|
-
|
509
|
-
area = form_value[0]
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
default_data = {'area': area,}
|
514
|
-
|
515
|
-
test_form = searchForm(initial = default_data)
|
516
|
-
|
517
|
-
q_context['test_form'] = test_form
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
logger.info(default_data)#Logging追記
|
522
|
-
|
523
|
-
logger.info(test_form)#Logging追記
|
524
|
-
|
525
|
-
logger.info(q_context)#Logging追記
|
526
|
-
|
527
|
-
return q_context
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
def get_queryset(self):
|
532
|
-
|
533
|
-
logger.info('Test3') #Logging追記
|
534
|
-
|
535
|
-
if 'form_value' in self.request.session:
|
536
|
-
|
537
|
-
form_value = self.request.session['form_value']
|
538
|
-
|
539
|
-
area = form_value[0]
|
540
|
-
|
541
|
-
condition_area = Q()
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
if len(area) != 0 and area[0]:
|
521
|
+
if len(restaurantDetailArea) != 0 and restaurantDetailArea[0]:
|
546
|
-
|
547
|
-
|
522
|
+
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
523
|
+
condition_restaurantDetailArea = Q(restaurantDetailArea__icontains=restaurantDetailArea) #<-ここがエラー引き起こしています。
|
552
|
-
|
553
|
-
|
554
|
-
|
524
|
+
|
555
|
-
else:
|
525
|
+
else:
|
556
|
-
|
526
|
+
|
557
|
-
|
527
|
+
pass
|
558
|
-
|
559
|
-
|
560
|
-
|
528
|
+
|
561
|
-
```
|
529
|
+
```
|
562
|
-
|
563
|
-
|
530
|
+
|
564
|
-
|
565
|
-
|
531
|
+
まずは、経過だけ書かせていただき、一休みしたらもうワントライしてみます。
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
```console
|
570
|
-
|
571
|
-
logger.info(form_value) -> ['渋谷']
|
572
|
-
|
573
|
-
logger.info(default_data) -> {'area': '渋谷'}
|
574
|
-
|
575
|
-
logger.info(test_form) -> <tr><th><label for="id_area">エリア:</label></th><td><input type="text" name="area" value="渋谷" required id="id_area"></td></tr>
|
576
|
-
|
577
|
-
logger.info(q_context) -> {'view': <restaurantsList.views.selectView object at 0x04734F50>, 'test_form': <searchForm bound=False, valid=False, fields=(area)>}
|
578
|
-
|
579
|
-
```
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
となると、def get_queryset(self):に該当する部分に問題があるということになるかと思うのですが、
|
584
|
-
|
585
|
-
それ以前に動きすらしていないので、原因がわからず......
|
1
views.pyにLoggingを仕込んで原因を深堀りしてみました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -433,3 +433,153 @@
|
|
433
433
|
OS:Windows10 home
|
434
434
|
|
435
435
|
```
|
436
|
+
|
437
|
+
|
438
|
+
|
439
|
+
**以下、2019/10/3追記**
|
440
|
+
|
441
|
+
views.pyにloggingモジュールを仕込んで、挙動を見てみました。
|
442
|
+
|
443
|
+
それでわかったのは、def文の1個目2個目は問題なく出力してくれており、
|
444
|
+
|
445
|
+
変数も差し支えなく渡されている模様でした。
|
446
|
+
|
447
|
+
|
448
|
+
|
449
|
+
しかし、def文の3個目は、どうにも動いてくれすらしていないようでした。
|
450
|
+
|
451
|
+
具体的には、以下のようにLoggingを仕込んだ結果、'Test3'と書かれたものが出力されませんでした。
|
452
|
+
|
453
|
+
|
454
|
+
|
455
|
+
```python
|
456
|
+
|
457
|
+
class selectView(TemplateView):
|
458
|
+
|
459
|
+
logger.info('Test1') #Logging追記
|
460
|
+
|
461
|
+
model = restaurantDetail
|
462
|
+
|
463
|
+
template_name = 'areaSelect.html'
|
464
|
+
|
465
|
+
|
466
|
+
|
467
|
+
def post(self, request, *args, **kwargs):
|
468
|
+
|
469
|
+
|
470
|
+
|
471
|
+
form_value=[
|
472
|
+
|
473
|
+
self.request.POST.get('area', None),
|
474
|
+
|
475
|
+
]
|
476
|
+
|
477
|
+
request.session['form_value'] = form_value
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
self.request.GET = self.request.GET.copy()
|
482
|
+
|
483
|
+
self.request.GET.clear()
|
484
|
+
|
485
|
+
|
486
|
+
|
487
|
+
logger.info(form_value) #Logging追記
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
return self.get(request, *args, **kwargs)
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
def get_context_data(self, **kwargs):
|
496
|
+
|
497
|
+
logger.info('Test2') #Logging追記
|
498
|
+
|
499
|
+
q_context = super().get_context_data(**kwargs)
|
500
|
+
|
501
|
+
area = ''
|
502
|
+
|
503
|
+
|
504
|
+
|
505
|
+
if 'form_value' in self.request.session:
|
506
|
+
|
507
|
+
form_value = self.request.session['form_value']
|
508
|
+
|
509
|
+
area = form_value[0]
|
510
|
+
|
511
|
+
|
512
|
+
|
513
|
+
default_data = {'area': area,}
|
514
|
+
|
515
|
+
test_form = searchForm(initial = default_data)
|
516
|
+
|
517
|
+
q_context['test_form'] = test_form
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
logger.info(default_data)#Logging追記
|
522
|
+
|
523
|
+
logger.info(test_form)#Logging追記
|
524
|
+
|
525
|
+
logger.info(q_context)#Logging追記
|
526
|
+
|
527
|
+
return q_context
|
528
|
+
|
529
|
+
|
530
|
+
|
531
|
+
def get_queryset(self):
|
532
|
+
|
533
|
+
logger.info('Test3') #Logging追記
|
534
|
+
|
535
|
+
if 'form_value' in self.request.session:
|
536
|
+
|
537
|
+
form_value = self.request.session['form_value']
|
538
|
+
|
539
|
+
area = form_value[0]
|
540
|
+
|
541
|
+
condition_area = Q()
|
542
|
+
|
543
|
+
|
544
|
+
|
545
|
+
if len(area) != 0 and area[0]:
|
546
|
+
|
547
|
+
condition_area = Q(area__icontains=area)
|
548
|
+
|
549
|
+
|
550
|
+
|
551
|
+
return restaurantDetail.objects.select_related().filter(condition_area)
|
552
|
+
|
553
|
+
|
554
|
+
|
555
|
+
else:
|
556
|
+
|
557
|
+
return restaurantDetail.objects.none()
|
558
|
+
|
559
|
+
|
560
|
+
|
561
|
+
```
|
562
|
+
|
563
|
+
ちなみに、仮にフォームに「渋谷」と書いた場合の出力は、
|
564
|
+
|
565
|
+
それぞれ以下のように出力されており、ちゃんと受け渡されている模様です。
|
566
|
+
|
567
|
+
|
568
|
+
|
569
|
+
```console
|
570
|
+
|
571
|
+
logger.info(form_value) -> ['渋谷']
|
572
|
+
|
573
|
+
logger.info(default_data) -> {'area': '渋谷'}
|
574
|
+
|
575
|
+
logger.info(test_form) -> <tr><th><label for="id_area">エリア:</label></th><td><input type="text" name="area" value="渋谷" required id="id_area"></td></tr>
|
576
|
+
|
577
|
+
logger.info(q_context) -> {'view': <restaurantsList.views.selectView object at 0x04734F50>, 'test_form': <searchForm bound=False, valid=False, fields=(area)>}
|
578
|
+
|
579
|
+
```
|
580
|
+
|
581
|
+
|
582
|
+
|
583
|
+
となると、def get_queryset(self):に該当する部分に問題があるということになるかと思うのですが、
|
584
|
+
|
585
|
+
それ以前に動きすらしていないので、原因がわからず......
|