回答編集履歴

5

修正

2020/05/07 09:11

投稿

ForestSeo
ForestSeo

スコア2722

test CHANGED
@@ -32,13 +32,17 @@
32
32
 
33
33
  model = Post
34
34
 
35
- queryset = Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
36
-
37
35
  template_name = 'post/index.html'
38
36
 
39
37
  context_object_name = 'post_data'
40
38
 
41
39
  paginate_by = 5
40
+
41
+
42
+
43
+ def get_queryset(self):
44
+
45
+ return Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
42
46
 
43
47
 
44
48
 

4

修正

2020/05/07 09:11

投稿

ForestSeo
ForestSeo

スコア2722

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
  model = Post
34
34
 
35
- queryset = Book.objects.filter(publisher__name='ACME Publishing')
35
+ queryset = Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
36
36
 
37
37
  template_name = 'post/index.html'
38
38
 

3

修正

2020/05/07 09:07

投稿

ForestSeo
ForestSeo

スコア2722

test CHANGED
@@ -32,6 +32,8 @@
32
32
 
33
33
  model = Post
34
34
 
35
+ queryset = Book.objects.filter(publisher__name='ACME Publishing')
36
+
35
37
  template_name = 'post/index.html'
36
38
 
37
39
  context_object_name = 'post_data'

2

追記

2020/05/07 09:06

投稿

ForestSeo
ForestSeo

スコア2722

test CHANGED
@@ -21,3 +21,37 @@
21
21
 
22
22
 
23
23
  ```
24
+
25
+ 追記
26
+
27
+ ```Python
28
+
29
+ # views.py
30
+
31
+ class IndexView(ListView):
32
+
33
+ model = Post
34
+
35
+ template_name = 'post/index.html'
36
+
37
+ context_object_name = 'post_data'
38
+
39
+ paginate_by = 5
40
+
41
+
42
+
43
+ def get_context_data(self, **kwargs):
44
+
45
+ context = super().get_context_data(**kwargs)
46
+
47
+ context['user'] = get_object_or_404(User, username=self.kwargs.get('username'))
48
+
49
+ return context
50
+
51
+
52
+
53
+ # urls.py
54
+
55
+ path("<str:username>/", views.IndexView.as_view()),
56
+
57
+ ```

1

修正

2020/05/07 08:19

投稿

ForestSeo
ForestSeo

スコア2722

test CHANGED
@@ -7,6 +7,8 @@
7
7
  template_name = 'index.html'
8
8
 
9
9
  def get_context_data(self, **kwargs):
10
+
11
+ context = super().get_context_data(**kwargs)
10
12
 
11
13
  user = get_object_or_404(User, username=self.kwargs.get('username'))
12
14