質問編集履歴
2
エラーメッセージの追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
django filter
|
1
|
+
django Userオブジェクトをfilterしてtemplateに渡したい
|
body
CHANGED
@@ -48,4 +48,6 @@
|
|
48
48
|
{% endfor %}
|
49
49
|
|
50
50
|
{% endblock %}
|
51
|
-
```
|
51
|
+
```
|
52
|
+

|
53
|
+

|
1
コードの追加
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
|
-
|
21
|
+
match_users = list(filter(example, users))
|
22
|
+
context = {
|
23
|
+
'match_users': match_users
|
24
|
+
}
|
25
|
+
|
10
|
-
|
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
|
+
```
|