Profile matching query does not exist.

I am trying to access a user’s profile, the user is logged in via admin but I get this error when trying to access the portal " DoesNotExist at /profile/2" - “Profile matching query does not exist.”.

How can I correct this please?

MODELS.PY

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(blank=True)
    follows = models.ManyToManyField("self", related_name="followed_by", symmetrical=False, blank=True)

    def __str__(self):
        return self.user

URLS.PY

    path("profile/<int:user_id>", views.profile, name="profile"),

PROFILE.HTML

{% extends "network/layout.html" %}

{% block body %}
<div class="col-5 profile__wrapper">
    <div class="header__wrapper">
      <!-- <div class="header_headerimg">
        <img src="http://placeimg.com/640/480/nature" alt="User background image" />
      </div> -->
      <div class="cols__container cols-container">
        <div class="content__col">
          <div class="img-container">
            <img src="http://placeimg.com/140/140/people" alt="User image" />
            <span></span>
          </div>
        <form method="POST">
            {% csrf_token %}
            <h2>{{ profile_user }}</h2>
            <p>@{{ profile_user }}</p>
            <input type="hidden" value="{{user.username}}" name="follower">
            <input type="hidden" value="{{user_object.username}}" name="user">

            <strong>Follows</strong> <br/>
            
            {% for following in profile.follows.all %}
                @ {{ following }} <br/>
            {% endfor %}

            <strong>Followed by</strong> <br/>
            {% for following in profile.followed_by.all %}
                @ {{ following }} <br/>
            {% endfor %}

        </form>
</div>
      </div>
    </div>
</div>
{% endblock %}

VIEWS.PY

def profile(request, user_id):
    if request.user.is_authenticated:
        profile = Profile.objects.get(pk = user_id)
        context = {
            "profile": profile,
        }
        return render(request, "network/profile.html", context)
    else:
        context = {
        "profile": profile,
    }
        return render(request, "network/profile.html", context)

The error I see is coming from Views - profile = Profile.objects.get(pk = user_id)

When I edit this to the below code, it shows but my profile name on Profile.html is blank i.e ’ {{ profile_user }}’

        profile = User.objects.get(pk = user_id)

The issue here is that the user field in Profile is not necessarily the same value as the id field in that instance.

This means that:

and

might be referring to two different objects.

If user_id is the pk of the User object, then you need to compare the foreign key in Profile to User.

In this case then it may look like:
Profile.objects.get(user_id=user_id)

This returns the same error

The error is specifically saying that you don’t have an instance of Profile that refers to that user.

What data do you have in the Profile model? (How many entries are in that table?) What user_ids are present?)

I checked, I had no profile data showing in my admin panel until I added one, is there a way for me to inherit data directly from the User Model when a user is created i.e I would like if when a user is created, it automatically creates a profile for that same user and stores the data in the Profile Model.

This may be my best option if the above is not available as I would not like for a user to need to create profiles manually after their user account is created for this particular project.

What do you think?

Yes, that’s an appropriate approach.

How are you creating users? Through the admin, or do you have other views to do this?

Through admin, I don’t have a separate view for this

In that case, you might want to use the get_or_create function for Profile instead of get. That will create the object the first time you try to reference it. It’s going to be easier to do it that way instead of adding something to the admin class.

1 Like