Django - i always get my profile when i what to view another users profile

Whenever i tried to view other people profile, it always return my own profile again, i don’t really know what’s wrong and i have tried calling request.user but it seems not to work

views.py

def UserProfile(request, username):
	user = get_object_or_404(User, username=username)
	profile = Profile.objects.get(user=user)
	url_name = resolve(request.path).url_name
	
	context = {
		'profile':profile,
		'url_name':url_name,
	}

	return render(request, 'userauths/profile.html', context)

urls.py main project

from userauths.views import UserProfile

urlpatterns = [
    path('admin/', admin.site.urls),
    path('users/', include('userauths.urls')),
    path('<username>/', UserProfile, name='profile'),

]

index.html

{% for creator in creators %}
     <a href="{% url 'profile' creator.user %}"><img class="avatar-img rounded-circle" src="{{creator.user.profile.image.url}}" alt="creators image" ></a></div>
     <h5 class="card-title"><a href="{% url 'profile' creator.user %}"> 
      {{creator.user.profile.first_name}} {{creator.user.profile.last_name}} {% if creator.verified %} <i class="bi bi-patch-check-fill text-info smsall" ></i> {% else %}{% endif %} </a></h5>
     <p class="mb-2">{{creator.user.profile.bio}}</p></div>
{% endfor %}

What is the view that creates a page using index.html?

Have you directly verified that you have the appropriate Profile object linked to the User object being retrieved for the username being used for testing?

What does your profile.html template look like?

1 Like

i have updated my question with the view.py that creates a page using index.html and also my profile.html, please if you can show me some code it would be of help. thanks

views.py (index.html)

def index(request):
    # user = request.user
    # creators = Profile.objects.filter(creator=True)
    creators = Profile.objects.filter(creator=True)

    context = {
        'creators': creatos,
    }
    return render(request, 'index.html', context)

index.html (Section where i am looping through creators)

{% for creator in creators %}
                            {% if creator.user.profile.first_name %}
                                <div class="card bg-transparent">
                                    <div class="card-body text-center">
                                        <div class="avatar" style="width:  150px; height: 150px; text-align: center; ">
                                            <a href="{% url 'profile' creator.user %}"><img class="avatar-img rounded-circle" src="{{creator.user.profile.image.url}}" alt="creators image" ></a>
                                        </div>
                                        <!-- Title -->
                                        <h5 class="card-title"><a href="">{{creator.user.profile.first_name}} {{creator.user.profile.last_name}} {% if creator.verified %} <i class="bi bi-patch-check-fill text-info smsall" ></i> {% else %}{% endif %} </a></h5>
                                        <p class="mb-2">{{creator.user.profile.bio}}</p>
                                    </div>
                                </div>
                            {% else %}
                            
                            {% endif %}
                            
                        {% endfor %}

profile.html

<div class="col-lg-8">
			
				<!-- Title -->
                {% if request.user.profile.first_name  %}
					<h5 class="mb-0">Hi, I am</h5>
                {% else %}
					<h5 class="mb-0">Hi, {{request.user|title}}, you need to <a href="{% url 'userauths:profile-update' %}" style="color: rgb(0, 119, 255);">update </a> your profile for more features and details</h5>
                {% endif %}
				<h1 class="mb-0">{{ request.user.profile.first_name }} {{ request.user.profile.last_name }}</h1>
				<p>{{ request.user.profile.bio }}</p>
				<!-- Content -->
				<p class="mt-4">{{ request.user.profile.aboutme }}</p>
				<!-- Personal info -->
				<ul class="list-group list-group-borderless">
					<li class="list-group-item px-0">
						<span class="h6 fw-light"><i class="fas fa-fw fa-map-marker-alt text-primary me-1 me-sm-3"></i>Address:</span>
						<span>{{ request.user.profile.country }}</span>
					</li>
					<li class="list-group-item px-0">
						<span class="h6 fw-light"><i class="fas fa-fw fa-envelope text-primary me-1 me-sm-3"></i>Email:</span>
						<span>{{ request.user.profile.email }}</span>
					</li>
					<li class="list-group-item px-0">
						<span class="h6 fw-light"><i class="fas fa-fw fa-headphones text-primary me-1 me-sm-3"></i>Phone number:</span>
						<span>{{ request.user.profile.phone }}</span>
					</li>
					<li class="list-group-item px-0">
						<span class="h6 fw-light"><i class="fas fa-fw fa-globe text-primary me-1 me-sm-3"></i>Website:</span>
						<span><a href="">{{ request.user.profile.website }}</a></span>
					</li>
				</ul>
				
				<!-- Counter START -->
				<div class="card-body px-3">
					
					<ul class="list-inline mb-0">
					  <li class="list-inline-item"> <a class="btn px-2 btn-sm bg-facebook" href="{{ request.user.profile.facebook }}"><i class="fab fa-fw fa-facebook-f"></i></a> </li>
					  <li class="list-inline-item"> <a class="btn px-2 btn-sm bg-instagram-gradient" href="{{ request.user.profile.instagram }}"><i class="fab fa-fw fa-instagram"></i></a> </li>
					  <li class="list-inline-item"> <a class="btn px-2 btn-sm bg-twitter" href="{{ request.user.profile.twitter }}"><i class="fab fa-fw fa-twitter"></i></a> </li>
					  <li class="list-inline-item"> <a class="btn px-2 btn-sm bg-linkedin" href="{{ request.user.profile.linkedin }}"><i class="fab fa-fw fa-linkedin-in"></i></a> </li>
					</ul>
				  </div>
				<br><br>
            </div>

i have updated my question with the view.py that creates a page using index.html and also my profile.html, please if you can show me some code it would be of help. thanks

Two questions for you:

What did you name the profile that you are setting in the context to display in the template?

What variables are you accessing in the template to display that profile information?

1 Like

Q1: it named profile, here’s a look

context = {
		'profile':profile,
	} 

Q2: i’m using {{ request.user.profile.first_name}} etc, to display the informations

What is request.user in a view?

1 Like

it refers to the actual user model instance, the logged in user, if i;m not mistaking!
i think i’m begining to get it now

Thanks alot, i fixed it now
I learnt something new about request.user and liked the way you though me

1 Like