No User matches a given query Django

While trying to display post-detail template in a blog app, I get an error No User matches a given query. In my models I am inheriting from User model. What seems the problem?

# Third party imports.
from django.contrib.auth.models import User
from django.db import models
from django.urls import reverse
from django.utils import timezone

class Post(models.Model):
        Creates Model for a post in the database.
    title = models.CharField(max_length=100)
    sub_title = models.CharField(max_length=100)
    content = models.TextField()
    date_posted = models.DateTimeField(
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    def __str__(self):
            A string representation of the post model.
        return self.title

    def get_absolute_url(self):
            Creates the absolute url for a particular post.
        return reverse('blog:post-detail', kwargs={'pk':})

class PostDetailView(DetailView):
        View for a post's details
    model = Post
    paginate_by = 3

    def get_queryset(self):
        user = get_object_or_404(User, username=self.kwargs.get('username'))
        return Post.objects.filter(author=user).order_by('-date_posted')

# Third party imports.
from django.urls import path
from .views import (

# Local application imports.
from . import views

# Specifies the app's name.
app_name = "blog"

urlpatterns = [
    path('', PostListView.as_view(), name="home"),  # url for post list.
         name="user-posts"),  # url for specific user post.
         name="post-detail"),  # url for post detail.
         name="post-create"),  # url to create new post.
         name="post-update"),  # url to update post.
         PostDeleteView.as_view(), name="post-delete"),  # url to delete post.
    path('about/', views.about, name="about"),  # url for about page.


{% extends 'blog/base.html' %}
{% load static %}
{% block crumb %}
{% for post in posts %}
<div class="post-preview">
<a href="{% url 'blog:post-detail' %}">
<h2 class="post-title">{{ post.title }}   </h2>
<h3 class="post-subtitle">{{ post.sub_title }}  </h3>

<p class="post-meta" style="font-weight: 300;"> Posted by 
  <a class="text-muted">{{}} on {{ post.date_posted|date:"F d, Y" }}</a>

<hr class="my-4" />
{% endfor %}

{% endblock %}


{% extends "blog/base.html" %}
{% block content %}
  <article class="media content-section">
    <img class="rounded-circle article-img"
         src="{{ }}">
    <div class="media-body">
      <div class="article-metadata">
        <a class="mr-2"
           href="{% url 'blog:user-posts' %}">{{ }}
        <small class="text-muted">{{ object.date_posted|date:"F d, Y" }}</small>
        {% if == user %}
            <a class="btn btn-secondary btn-sm mt-1 mb-1"
               href="{% url 'blog:post-update' %}">Update
            <a class="btn btn-danger btn-sm mt-1 mb-1"
               href="{% url 'blog:post-delete' %}">Delete
        {% endif %}
      <h2 class="article-title">{{ object.title }}</h2>
      <p class="article-content">{{ object.content }}</p>
{% endblock content %}

Some notes about things that are wrong that aren’t directly related to the error you’re getting.

  • You’re using a DetailView (which shows the details for a single object) where it looks like you want a ListView (to list an user’s posts).
    • Your context_object_name is in plural, which implies you do want a list of things.
    • paginate_by has no effect in DetailViews, since a single-object view couldn’t be paginated.
  • Your view refers to a blog/post_detail.html, while you’ve pasted in a home.html. Which one is it?
  • You say “In my models I am inheriting from User model.”, but it looks like you’re not importing a custom User model, but the default from django.contrib.auth.models import User. If you really are using a custom user model that inherits from BaseUser (not User), then that’s wrong too.

The error itself, I imagine (since you’re not supplying us with the traceback), comes from

user = get_object_or_404(User, username=self.kwargs.get('username'))

which means you’re not passing in an <username> in the URL for that view, or the <username> you’re using is incorrect (and there is no such user). (You’re not showing us your, so that’s just a guess.)

(A Detail view’s URL, in any case, would probably not refer to an username, but the unique identifier of the post.)


Following the edit of the original post, the issue is that the get_queryset() is simply extraneous (since no filtering by username is required) in that DetailView, so the class can be simplified to:

class PostDetailView(DetailView):
    model = Post

Leave a Comment