teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

エラーメッセージの追加

2020/06/08 23:58

投稿

tonytony
tonytony

スコア11

title CHANGED
@@ -1,1 +1,1 @@
1
- django filter関数およびall()でエラーが出てまう問題
1
+ django Userオブジェクトをfilterしてtemplate渡した
body CHANGED
@@ -48,4 +48,6 @@
48
48
  {% endfor %}
49
49
 
50
50
  {% endblock %}
51
- ```
51
+ ```
52
+ ![イメージ説明](1bfe45b8a4008c4513400a7c7d45dbd4.png)
53
+ ![イメージ説明](d115fdfc0f65a3da3d448f5751ea4071.png)

1

コードの追加

2020/06/08 23:58

投稿

tonytony
tonytony

スコア11

title CHANGED
File without changes
body CHANGED
@@ -4,7 +4,48 @@
4
4
  デバッグツールを回したところ、数値の取得はできていると思います。
5
5
 
6
6
  users変数に格納されているデータ型が悪いのでしょうか?
7
+ view内でやっていることは、Userオブジェクトを全てリストにしてusersに代入し、それをfilterにかけたあと、match_usersにlist型にして格納。その後それをcontextに渡している。という感じです。
7
8
  知見のある方、お力貸していただけると幸いです。
8
9
 
10
+ ```viws.py
11
+ class HomeView(LoginRequiredMixin, View):
12
+
13
+ def get(self, request, *args, **kwargs):
14
+ # 自分以外、異性、すでにいいね悪いねしてない人の三つで絞る
15
+ def example(user):
16
+ if user.id != request.user.id and user.profile.gender != request.user.profile.gender:#すでにいいねしている人を省く
17
+ return True
18
+ else:
19
+ return False
20
+ users = list(User.objects.all())
9
- ![イメージ説明](6258353d9479ed86734d3996e46ef480.png)
21
+ match_users = list(filter(example, users))
22
+ context = {
23
+ 'match_users': match_users
24
+ }
25
+
10
- ![イメージ説明](2f78e4ad2000dd486e0749885149a14c.png)
26
+ return render(request, 'home.html', context)
27
+ ```
28
+
29
+ ```home.html
30
+ <!doctype html>
31
+ {% extends "base.html" %}
32
+
33
+ {% block content %}
34
+
35
+ <title>pairnite(仮称)</title>
36
+ </head>
37
+ <body>
38
+ <h1>ここはログイン後の一覧ページです。</h1>
39
+ <div>
40
+ <h3>{{ request.user.profile.user_name }}</h3>
41
+ <a class="btn btn-primary" href="{% url 'users:detail' request.user.pk %}" role="button">自分の詳細ページ</a>
42
+ </div>
43
+ {% for request.user in request.user %}
44
+ { request.user.profile.user_name }
45
+ {% endfor %}
46
+ {% for match_user in match_users_lists %}
47
+ { match_user.profile.user_name }
48
+ {% endfor %}
49
+
50
+ {% endblock %}
51
+ ```