0

I have the following code in my userprofile_list.html template:

{% extends "base.html" %} {% block content %} {% for users in userprofile_list %} <a href="{% url 'users:user_profile' user.pk %}"> <div class="user-card"> <img class="profile-pic" src="{%if user.userprofile.profile_pic%}{{user.userprofile.profile_pic.url}}{%endif%}"> <p class="user-card-name">{{ users.pk }}</p> <p class="user-card-name">{{ users.first_name }}</p> <p class="user-card-type">{{ users.user_type }}</p> </div> </a> {% endfor %} {% endblock %} 

Note the line <a href="{% url 'users:user_profile' user.pk %}">, I am using it elsewhere in the my app and when clicked it takes you to the profile of the currently logged-in user. However, I would instead like it to take you to the profile of whichever user you clicked on in the users list being created by the for loop. How do I change the url to do that?

I think what has to be done is that instead of getting the pk of the currently logged in user it has to instead get the pk of that specific user in the users list, which is then passed through to the url patterns (already working so I didn't posting it).

Note: If I'm not right with my logic on what has to happen thats fine just let me know what you need to see in order to help. Thank you.

Edit

base.html:

<!DOCTYPE html> {% load staticfiles %} <html lang="en"> <head> <title>Evverest</title> <meta name"viewport" content="width=device-width, initial-scale=1"> <meta charset="uft-8"> <link rel="shortcut icon" href="/images/favicon.ico"> <link href="https://fonts.googleapis.com/css?family=Droid+Sans|Mukta+Mahee|Noto+Sans" rel="stylesheet"> <link rel="stylesheet" href="{% static 'css/style.css' %}"> </head> <body> <nav> <div class="container"> <a class="brand" href="{% url 'index' %}">Evverest</a> <div class="navbar"> <a class="nav-link" href="{% url 'index' %}">Home</a> {% if user.is_authenticated %} <a class="nav-link" href="{% url 'users:user_profile' user.id %}"> {{ user.username|capfirst }} </a> <a class="nav-link" href="{% url 'users:user_list' %}">All Members</a> <a class="nav-link" href="{% url 'account_logout' %}">Logout</a> {% else %} <a class="nav-link" href="{% url 'account_login' %}?next=/">Login</a> <a class="nav-link" href="{% url 'account_signup' %}?next=/">Register</a> {% endif %} </div> </div> </nav> <div class="container"> <div class="content"> {% block content %} {% endblock %} </div> </div> </body> </html> 
0

1 Answer 1

1

I think you have a typo in that link. Try this:

 <a href="{% url 'users:user_profile' users.pk %}"> 

You are using user.pk The loop variable is users, so you need to use users.pk, assuming that you have created a view to edit user_profile by getting pk of user_profile .

EDIT Try this base.html:

 <!DOCTYPE html> {% load staticfiles %} <html lang="en"> <head> <title>Evverest</title> <meta name"viewport" content="width=device-width, initial-scale=1"> <meta charset="uft-8"> <link rel="shortcut icon" href="/images/favicon.ico"> <link href="https://fonts.googleapis.com/css?family=Droid+Sans|Mukta+Mahee|Noto+Sans" rel="stylesheet"> <link rel="stylesheet" href="{% static 'css/style.css' %}"> </head> <body> <nav> <div class="container"> <a class="brand" href="{% url 'index' %}">Evverest</a> <div class="navbar"> <a class="nav-link" href="{% url 'index' %}">Home</a> {% if request.user.is_authenticated %} <a class="nav-link" href="{% url 'users:user_profile' request.user.id %}"> {{ request.user.username|capfirst }} </a> <a class="nav-link" href="{% url 'users:user_list' %}">All Members</a> <a class="nav-link" href="{% url 'account_logout' %}">Logout</a> {% else %} <a class="nav-link" href="{% url 'account_login' %}?next=/">Login</a> <a class="nav-link" href="{% url 'account_signup' %}?next=/">Register</a> {% endif %} </div> </div> </nav> <div class="container"> <div class="content"> {% block content %} {% endblock %} </div> </div> 

Sign up to request clarification or add additional context in comments.

8 Comments

So that works, but then it causes another problem. It makes the list of users buttons that go to each of their profiles. However, the logged in user is supposed to be displayed in the nav bar and when it brings me to a new user's profile but instead it displays that user is the nav bar instead of the logged in user. Here's the nabber code: <a class="nav-link" href="{% url 'users:user_profile' user.id %}"> {{ user.username|capfirst }} </a>
That's the nav bar link to display the currently logged in user. If I'm logged in as Garrett but I go to Marc's profile that link in the nav bar will display Marc (and go to Marc's profile) when it should still display Garrett (or whichever user is logged in)
Replace the user.id with request.user.id in the navbar link and use request.user.username instead of user.username. The reason for this is because I think you made a context variable user for the view. request.user returns the currently logged in user. Update if this worked.
It still changes depending on which ever profile you are viewing.
Can you include the base.html too in the question?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.