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

Time:01-07

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  src="{{creator.user.profile.image.url}}" alt="creators image" ></a></div>
     <h5 ><a href="{% url 'profile' creator.user %}"> 
      {{creator.user.profile.first_name}} {{creator.user.profile.last_name}} {% if creator.verified %} <i  ></i> {% else %}{% endif %} </a></h5>
     <p >{{creator.user.profile.bio}}</p></div>
{% endfor %}

CodePudding user response:

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

CodePudding user response:

Instead of this:

<a href="{% url 'profile' creator.user %}">

you should try:

<a href="{% url 'profile' creator.user.username %}">

From the presumably working statement get_object_or_404(User, username=username) I assume that username is a field (hopefully, unique) of the User. So you should pass it onto the URL, not the entire User model.

  •  Tags:  
  • Related