Skip to content
Advertisement

Other user’s profile is not showing, It is showing only current user name . How to display other user’s profile?

I am creating a web app and I want this to display other user’s profile through the user’s post. I mean when a user click on other user’s profile for see the other user’s information then it will show the other user’s profile. I’ve tried everything but it is showing the name of the current user information after click on the post.

Please Help me in this. Thank you very Much. I will really apppreciate your Help.

Here is my Code:-

views.py

def post_profile(request,username):
    poste = Profile.objects.get(user=request.user)
    context = {'poste':poste}
    return render(request, 'mains/post_profile.html', context)

models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE,default='',unique=True)
    full_name = models.CharField(max_length=100,default='')
    date_added = models.DateTimeField(auto_now_add=True)

Please help me in this, I don’t know where is the problem

Advertisement

Answer

You can try this:

Create a profile url and pass the user_id parameter with it.

urls.py

from django.urls import path

urlpatterns =[
    ....
    path('profile/<int:user_id>', views.show_profile, name='show_profile'),
]

Now create a view and pass the user_id parameter with the request parameter to it.

def show_profile(request, user_id):
    poste = Profile.objects.get(user=user_id)
    context = {'poste': poste}
    return render(request, 'mains/post_profile.html', context)

Now show the user profile in the template.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement