Home > Net >  django - No User matches the given query. Page Not found 404: User post list view breaks post detail
django - No User matches the given query. Page Not found 404: User post list view breaks post detail

Time:09-20

I'm fairly new to django and trying to build a message board app. Everything's been running smoothly up until I tried to add functionality to display a list of posts by author. I got it working but then my post detail view started throwing a 'No User matches the given query. Page Not found 404' error and I can't seem to figure out why. Can anyone help?

views.py

class UserPostList(generic.ListView):
    
    model = Post
    # queryset = Post.objects.filter(status=1).order_by('-created_on')
    template_name = 'user_posts.html'
    paginate_by = 6

    def get_queryset(self):
        """
        Method to return posts restricted to 'published' status AND to
        authorship by the user whose username is the parameter in the
        url.
        """

        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(
            status=1, author=user
            ).order_by('-created_on')


class FullPost(View):
    
    def get(self, request, slug, *args, **kwargs):
        """
        Method to get post object.
        """

        queryset = Post.objects.filter(status=1)
        post = get_object_or_404(queryset, slug=slug)
        comments = post.comments.order_by('created_on')
        liked = False
        if post.likes.filter(id=self.request.user.id).exists():
            liked = True

        return render(
            request,
            "full_post.html",
            {
                "post": post,
                "comments": comments,
                "liked": liked
            },
        )

    # I'll be adding a comment form in here too
       

urls.py

urlpatterns = [
    ...
    path('<slug:slug>/', views.FullPost.as_view(), name='boards_post'),
    ...
    path('<str:username>/', views.UserPostList.as_view(), name='user_posts'),
    ...
]

Error message (When trying to view a single post (previously working) after adding the UserPostList view and route)

Using the URLconf defined in mhcmsgboard.urls, Django tried these URL patterns, in this order:

    1. admin/
    2. summernote/
    3. register/ [name='register']
    4. profile/ [name='profile']
    5. login/ [name='login']
    6. logout/ [name='logout']
    7. new/ [name='create_post']
    8. <slug:slug>/update/ [name='update_post']
    9. <slug:slug>/delete/ [name='delete_post']
    10. <str:username>/ [name='user_posts']

The current path, test-post/, matched the last one.

CodePudding user response:

The problem is that you match update_post, delete_post and user_posts URL's to the root. As the error message explains, the current request is made against user_posts. And it seems that you don't have a user called test-post. You could solve it e.g. with the following URL conf:

urlpatterns = [
    ...
    path('board/<slug:slug>/', views.FullPost.as_view(), name='boards_post'),
    ...
    path('posts/<str:username>/', views.UserPostList.as_view(), name='user_posts'),
    ...
]

That way, each path is unique and Django knows which View it has to call.

CodePudding user response:

for <str:name> in path "-" not allowed to use in name, when you use Slug both paths are equal.

urlpatterns = [
   ...
   path('<slug:slug>/', views.FullPost.as_view(), name='boards_post'),
   ...
   path('<slug:username>/', views.UserPostList.as_view(), name='user_posts'),
]

there are 2 simple ways

  1. use sub path for one or both paths : user_post/<slug:username>/
  2. Use re_path and define the regex to change path

exp: re_path(r'^(?P<username>\w )/$', views.UserPostList.as_view()),

  • Related