My detail view api is outputting error

# 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?

Side note: When posting code here, please enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. (I’ve taken the liberty of editing this post for you.)

What specifically is the error you are receiving? Please include the complete traceback from the console. (Surrounded by ``` as described in my previous reply.)

Another observation - I don’t believe this entire set of views is going to do what you’re trying to do. It’s not really clear what you’re trying to achieve here, but it really seems to me that whatever you’re trying to accomplish, this isn’t the way to do it.

A page consists of a view receiving a request and returning a response. When you’re handling a form, the general flow is that the form submits to the same page that renders it.

As background, have you worked your way through either (or both) of the Official Django Tutorial or the Django Girls Tutorial?

Thanks for the response and edit.

"GET /api/v1/1 HTTP/1.1" 404 5813

This is the response I keep getting. I am struggling to get the detail view of the API.

The 404 indicates that that url is not mapped to a view. What does your urls.py file look like for that?

I have not successfully found the section of the Official Django Tutorial that subclasses a class of Comments (get and post) into (another class) a detail view class.

That’s because that’s not how you approach this. Think of the page you’re trying to create, and the view you’re writing to create it, as a complete “thing” and not as the combination of parts. You’ll end up writing one view that has all these different components on it.

1 Like

It is surprising because all the other views work perfectly in my API, save the detail view.

from django.urls import path

from .views import (
    BlogListView,
    PostDetailView,
    AboutPageView,
    FaithListView,
    LeadershipListView,
    MemoriesListView,
    EntrepreneurshipListView,
    CultureListView,
)

urlpatterns = [
    path("about/", AboutPageView.as_view(), name="about"),
    path("faith/", FaithListView.as_view(), name="category_faith"),
    path("leadership/", LeadershipListView.as_view(), name="category_leadership"),
    path("memories/", MemoriesListView.as_view(), name="category_memories"),
    path(
        "entrepreneurship/",
        EntrepreneurshipListView.as_view(),
        name="category_entrepreneurship",
    ),
    path("culture/", CultureListView.as_view(), name="category_culture"),
    path("<uuid:pk>/", PostDetailView.as_view(), name="post_detail"),
    path("", BlogListView.as_view(), name="home"),
]

This is my API app’s URL file.

Thanks for the input. I thought as much. I will work through this again. Any hint on how to go about it?

Start by identifying everything you want on the page. (Not as code, but just knowing what all you want on the page.)

Then decide whether you’re going to create this as a function-based view or a class-based view. If you’re going to use a class-based view, decide which class you’re going to use as a base class.

Then write the code to retrieve all the data necessary to create the page to be rendered - and if a form is involved, the code necessary to handle the form submission.
(The precise manner you use to pull all this code together depends upon the type of view.)

Finally, make sure that all the data you want rendered is in the context, render a response, and return it.

These are the basic steps you should go through for every view - it’s not unique to this situation.

1 Like

Thanks a lot. In contrast, my code was OK. I did take to your advice by refactoring the views into one class-based view. The issue resulted from entering the id for each post instance instead of using their uuid as specified in my model. Thanks for your advice.