質問編集履歴
3
viewに追加のコードを記載
test
CHANGED
File without changes
|
test
CHANGED
@@ -162,6 +162,34 @@
|
|
162
162
|
|
163
163
|
```
|
164
164
|
|
165
|
+
from django.shortcuts import redirect
|
166
|
+
|
167
|
+
from django.views.generic import ListView, DetailView
|
168
|
+
|
169
|
+
from .forms import CreateRoomForm
|
170
|
+
|
171
|
+
from accounts.models import User
|
172
|
+
|
173
|
+
from .models import Room, Message
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
class TeacherListView(ListView):
|
178
|
+
|
179
|
+
model = User
|
180
|
+
|
181
|
+
template_name = 'index.html'
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
def get_queryset(self):
|
186
|
+
|
187
|
+
queryset = User.objects.filter(is_teacher=True)
|
188
|
+
|
189
|
+
return queryset
|
190
|
+
|
191
|
+
|
192
|
+
|
165
193
|
class RoomMessagesListView(DetailView):
|
166
194
|
|
167
195
|
model = Message
|
2
エラー内容変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
index.htmlからas_hiddenを使用し、viewsにpkを渡したいのだが
|
1
|
+
index.htmlからas_hiddenを使用し、viewsにpkを渡したいのだがprint(form.cleaned_data.get('pk'))がNoneになる
|
test
CHANGED
@@ -150,13 +150,9 @@
|
|
150
150
|
|
151
151
|
|
152
152
|
|
153
|
-
そしてここで、テンプレートから渡されたteacherの**pk**とログイン中のcustomerの**pk**を使用しfilterにかけたいのですが、
|
153
|
+
そしてここで、テンプレートから渡されたteacherの**pk**とログイン中のcustomerの**pk**を使用しfilterにかけたいのですが、`print(form.cleaned_data.get('pk')`を試すと出力結果がNoneになります。
|
154
|
-
|
155
|
-
|
154
|
+
|
156
|
-
|
157
|
-
このコードの記述方法が間違ってると思われるのですが、どう変更すればいいのでしょうか?
|
158
|
-
|
159
|
-
|
155
|
+
`{{ form.object.teacher.pk.as_hidden }}`おそらくこちらの記述方法が間違ってると思ってます。
|
160
156
|
|
161
157
|
ご教授頂ければ幸いです。よろしくお願い致します。
|
162
158
|
|
1
モデル情報追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -6,10 +6,74 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
+
[accounts/models.py]
|
10
|
+
|
11
|
+
```
|
12
|
+
|
13
|
+
from django.contrib.auth.models import AbstractUser
|
14
|
+
|
15
|
+
from django.db import models
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
class User(AbstractUser):
|
20
|
+
|
21
|
+
is_customer = models.BooleanField(default=False)
|
22
|
+
|
23
|
+
is_teacher = models.BooleanField(default=False)
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
class Customer(models.Model):
|
28
|
+
|
29
|
+
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
|
30
|
+
|
31
|
+
age = models.CharField(verbose_name='年齢', blank=True, null=True, max_length=10)
|
32
|
+
|
33
|
+
personality = models.TextField(verbose_name='アピールポイント', blank=True, null=True, max_length=1000)
|
34
|
+
|
35
|
+
profile_picture = models.ImageField(verbose_name = 'プロフィール画像', blank=True, null=True, upload_to='images/', default='default_user_icon.png')
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
def __str__(self):
|
40
|
+
|
41
|
+
return self.user.username
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
class Teacher(models.Model):
|
46
|
+
|
47
|
+
user = models.OneToOneField(User, on_delete = models.CASCADE, primary_key = True)
|
48
|
+
|
49
|
+
age = models.CharField(verbose_name='年齢', blank=True, null=True, max_length=10)
|
50
|
+
|
51
|
+
personality = models.TextField(verbose_name='アピールポイント', blank=True, null=True, max_length=1000)
|
52
|
+
|
53
|
+
profile_picture = models.ImageField(verbose_name = 'プロフィール画像', blank=True, null=True, upload_to='images/', default='default_user_icon.png')
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
def __str__(self):
|
58
|
+
|
59
|
+
return self.user.username
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
```
|
64
|
+
|
9
65
|
[models.py]
|
10
66
|
|
11
67
|
```
|
12
68
|
|
69
|
+
from django.db import models
|
70
|
+
|
71
|
+
from django.conf import settings
|
72
|
+
|
73
|
+
from accounts.models import User, Customer, Teacher
|
74
|
+
|
75
|
+
|
76
|
+
|
13
77
|
class Room(models.Model):
|
14
78
|
|
15
79
|
teacher_id = models.ForeignKey(Teacher, null=True, blank=True, on_delete=models.CASCADE)
|
@@ -118,24 +182,24 @@
|
|
118
182
|
|
119
183
|
if form.is_valid():
|
120
184
|
|
121
|
-
room_data = Room.objects.filter(teacher_id=form.cleaned_data
|
185
|
+
room_data = Room.objects.filter(teacher_id=form.cleaned_data.get('pk'), customer_id=request.user.pk)
|
122
|
-
|
186
|
+
|
123
|
-
if room_data:
|
187
|
+
if room_data is not None:
|
188
|
+
|
189
|
+
return redirect('room', pk=room_data[0].pk)
|
190
|
+
|
191
|
+
else:
|
192
|
+
|
193
|
+
room_data = Room()
|
194
|
+
|
195
|
+
room_data.teacher_id = form.cleaned_data.get('pk')
|
196
|
+
|
197
|
+
room_data.customer_id = request.user.pk
|
198
|
+
|
199
|
+
room_data.save()
|
124
200
|
|
125
201
|
return redirect('room', pk=room_data.pk)
|
126
202
|
|
127
|
-
else:
|
128
|
-
|
129
|
-
room_data = Room()
|
130
|
-
|
131
|
-
room_data.teacher_id = form.cleaned_data['pk']
|
132
|
-
|
133
|
-
room_data.customer_id = request.user.pk
|
134
|
-
|
135
|
-
room_data.save()
|
136
|
-
|
137
|
-
return redirect('room', pk=room_data.pk)
|
138
|
-
|
139
203
|
|
140
204
|
|
141
205
|
```
|