Garrett
Garrett

Reputation: 1792

URL to each user's profile in a list of users

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>

Upvotes: 0

Views: 290

Answers (1)

Ajmal Noushad
Ajmal Noushad

Reputation: 946

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>

Upvotes: 1

Related Questions