[Django]-Generic detail view PostDetailView must be called with either an object pk or a slug in the URLconf

3👍

Your url should have pk arg instead of new_str

path('details/<int:pk>/', PostDetailView.as_view(), name="post_details"),

1👍

You need to rewrite get_object

view.py

class PostDetailView(DetailView):
    model = Post
    template_name = 'blogs/blog_details.html'
    
    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        new_str = self.kwargs.get('new_str') or self.request.GET.get('new_str') or None

        queryset = queryset.filter(pk=new_str)
        obj = queryset.get()
        return obj

see http://www.chenxm.cc/article/1143.html

-1👍

view.py

class PostDetailView(DetailView):
    model = Post
    template_name = 'blogs/blog_details.html'
    
    def get_object(self, queryset=None):
        if queryset is None:
            queryset = self.get_queryset()
        new_str = self.kwargs.get('new_str') or self.request.GET.get('new_str') or None

        queryset = queryset.filter(pk=new_str)
        obj = queryset.get()
        return obj

see http://www.chenxm.cc/article/1143.html

Leave a comment