# Project's View.py
class CommentGet(DetailView):
model = Post
template_name = "post_detail.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = CommentForm()
return context
class CommentPost(SingleObjectMixin, FormView):
model = Post
form_class = CommentForm
template_name = "post_detail.html"
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def form_valid(self, form):
comment = form.save(commit=False)
comment.post = self.object
comment.save()
return super().form_valid(form)
def get_success_url(self):
post = self.get_object()
return reverse("post_detail", kwargs={"pk": post.pk})
class PostDetailView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
view = CommentGet.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = CommentPost.as_view()
return view(request, *args, **kwargs)
# BlogAPi Views.py
class CommentGet(DetailView):
queryset = Post.objects.all()
serializer_class = PostSerializer
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"] = CommentForm()
return context
class CommentPost(SingleObjectMixin, FormView):
queryset = Post.objects.all()
serializer_class = PostSerializer
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def form_valid(self, form):
comment = form.save(commit=False)
comment.post = self.object
comment.save()
return super().form_valid(form)
def get_success_url(self):
post = self.get_object()
return reverse("post_detail", kwargs={"pk": post.pk})
class PostDetailView(LoginRequiredMixin, View):
def get(self, request, *args, **kwargs):
view = CommentGet.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = CommentPost.as_view()
return view(request, *args, **kwargs)
I passed the proper details into each view, but I am lost on the detail view page. My API is working perfectly, save for the detail view page. I honestly am confused. Any help, please?