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

回答編集履歴

5

修正

2020/05/07 09:11

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -15,11 +15,13 @@
15
15
  # views.py
16
16
  class IndexView(ListView):
17
17
  model = Post
18
- queryset = Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
19
18
  template_name = 'post/index.html'
20
19
  context_object_name = 'post_data'
21
20
  paginate_by = 5
22
21
 
22
+ def get_queryset(self):
23
+ return Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
24
+
23
25
  def get_context_data(self, **kwargs):
24
26
  context = super().get_context_data(**kwargs)
25
27
  context['user'] = get_object_or_404(User, username=self.kwargs.get('username'))

4

修正

2020/05/07 09:11

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -15,7 +15,7 @@
15
15
  # views.py
16
16
  class IndexView(ListView):
17
17
  model = Post
18
- queryset = Book.objects.filter(publisher__name='ACME Publishing')
18
+ queryset = Post.objects.filter(user=get_object_or_404(User, username=self.kwargs.get('username'))
19
19
  template_name = 'post/index.html'
20
20
  context_object_name = 'post_data'
21
21
  paginate_by = 5

3

修正

2020/05/07 09:07

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -15,6 +15,7 @@
15
15
  # views.py
16
16
  class IndexView(ListView):
17
17
  model = Post
18
+ queryset = Book.objects.filter(publisher__name='ACME Publishing')
18
19
  template_name = 'post/index.html'
19
20
  context_object_name = 'post_data'
20
21
  paginate_by = 5

2

追記

2020/05/07 09:06

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -9,4 +9,21 @@
9
9
  context['post_data'] = user.post_set.all()
10
10
  return context
11
11
 
12
+ ```
13
+ 追記
14
+ ```Python
15
+ # views.py
16
+ class IndexView(ListView):
17
+ model = Post
18
+ template_name = 'post/index.html'
19
+ context_object_name = 'post_data'
20
+ paginate_by = 5
21
+
22
+ def get_context_data(self, **kwargs):
23
+ context = super().get_context_data(**kwargs)
24
+ context['user'] = get_object_or_404(User, username=self.kwargs.get('username'))
25
+ return context
26
+
27
+ # urls.py
28
+ path("<str:username>/", views.IndexView.as_view()),
12
29
  ```

1

修正

2020/05/07 08:19

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -3,6 +3,7 @@
3
3
  class IndexView(TemplateView):
4
4
  template_name = 'index.html'
5
5
  def get_context_data(self, **kwargs):
6
+ context = super().get_context_data(**kwargs)
6
7
  user = get_object_or_404(User, username=self.kwargs.get('username'))
7
8
  context['user'] = user
8
9
  context['post_data'] = user.post_set.all()