herrypotter
herrypotter

Reputation: 95

accessing a different user's information in django

I know to access the logged-in user's data we use request.user. My goal is to list all the users in the table and have a link to their profile page. How do I make the link go to the user's profile page?

I have the following:

# app/views.py
def tutors_list(request):
  user_list = CustomUser.objects.all()
  context = {
    'user_list': user_list
  }  
  return render(request, 'tutors_list.html', context)

def show_profile(request, username):
  user = CustomUser.objects.get(username = username) ### NOT DISPLAYING RIGHT USER
  #user = CustomUser.objects.get(id=id)
  context = {
    'user': user
  }  
  return render(request, 'show_profile.html', context)


# myproject/urls.py
url_patterns = [
  # ...
  path('show_profile/', views.show_profile, name='show_profile'),
  # ...

I'm getting an error saying show_profile is expecting 1 more argument, username. How exactly would this model work if I need to pull data for a specific user in the database and not the logged-in user?

Upvotes: 0

Views: 366

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

As your error says show_profile is expecting 1 more argument, username, so you need to pass username in your url pattern:

 path('<str:username>/show_profile/', views.show_profile, name='show_profile'),

Upvotes: 1

Related Questions