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)