実現したいこと
paginate_byを使用したいため、class内のTemplateViewからListViewに変更したいです。
どなたかご教授頂けると幸いです。
該当のソースコード
python
1class Sample(LoginRequiredMixin, TemplateView): 2 #model = Post 3 paginate_by = 5 4 template_name = 'blog/sample.html' 5 6 def get(self, request, *args, **kwargs): 7 context = super().get_context_data(**kwargs) 8 context['member'] = Member.objects.get(user=self.request.user) 9 context['object_list'] = Post.objects.filter(status=Status.objects.get(name=self.kwargs.get('stat'))).order_by('id').reverse() 10 flag = 0 11 12 for s in Member.objects.get(user=self.request.user).status.all(): 13 if(self.kwargs.get('stat') == str(s)): 14 flag += 1 15 16 if flag != 1: 17 return redirect('authentication:home') 18 else: 19 return render(request, 'blog/sample.html', context)
試したこと
python
1def get(self, request, *args, **kwargs): 2 context = super().get_context_data(**kwargs) 3 context['member'] = Member.objects.get(user=self.request.user) 4 #context['object_list'] = Post.objects.filter(status=Status.objects.get(name=self.kwargs.get('stat'))).order_by('id').reverse() 5 flag = 0 6 7 for s in Member.objects.get(user=self.request.user).status.all(): 8 if(self.kwargs.get('stat') == str(s)): 9 flag += 1 10 11 if flag != 1: 12 return redirect('authentication:home') 13 else: 14 return render(request, 'blog/sample.html', context) 15 16 def get_queryset(self, **kwargs): 17 return Post.objects.filter(status=Status.objects.get(name=self.kwargs.get('stat'))).order_by('id').reverse()
追記
python
1class Sample(LoginRequiredMixin, ListView): 2 #model = Post 3 paginate_by = 5 4 template_name = 'blog/sample.html' 5 6 def get_queryset(self, **kwargs): 7 print('get_queryser') 8 return Post.objects.filter(status=Status.objects.get(name=self.kwargs.get('stat'))).order_by('id').reverse() 9 10 def get_context_data(self, **kwargs): 11 print('get_context_data') 12 context = super().get_context_data(**kwargs) 13 context['member'] = Member.objects.get(user=self.request.user) 14 print('get_context_data') 15 return context 16 17 def get(self, request, *args, **kwargs): 18 print('get') 19 #context = super().get_context_data(**kwargs) 20 #context['member'] = Member.objects.get(user=self.request.user) 21 #context['object_list'] = Post.objects.filter(status=Status.objects.get(name=self.kwargs.get('stat'))).order_by('id').reverse() 22 flag = 0 23 24 for s in Member.objects.get(user=self.request.user).status.all(): 25 if(self.kwargs.get('stat') == str(s)): 26 flag += 1 27 28 if flag != 1: 29 return redirect('authentication:home') 30 else: 31 return render(request, 'blog/sample.html')
追記コンソール画面
shell
1System check identified no issues (0 silenced). 2April 02, 2020 - 12:23:15 3Django version 3.0.1, using settings 'mysite.settings' 4Starting development server at http://127.0.0.1:8000/ 5Quit the server with CTRL-BREAK. 6get 7[02/Apr/2020 12:23:24] "GET /blog/Vlog/ HTTP/1.1" 200 1770
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。