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

0

Issue

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 %}

Solution

I just fixed it now, in my profile.html, i am using {{request.user.profile.first_name}} to display the informations. I was supposed to use {{profile.first_name}} etc, and that was it

Thanks to everyone who helped

Answered By – Destiny Franks

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More