MaxDragonheart
MaxDragonheart

Reputation: 1290

Incorrect redirect to user profile details

If the user is a staff' member he will see the list of users and their profile details, in addition to his profile details. If not, he will see only his profile details.

models.py

class UserProfile(AbstractUser):

    def __str__(self):
        return self.username

    def get_absolute_url(self):
        return reverse("user_details", kwargs={"username":self.username})

views.py

def usersList(request):
    """
    The list of all users
    """
    if request.user.is_superuser:
        users_list = UserProfile.objects.all()
    elif request.user.is_staff:
        users_list = UserProfile.objects.filter(is_staff=False)
    else:
        raise PermissionDenied
    template = 'usermanager/users_list.html'
    context = {
        'users': users_list,
    }
    return render(request, template, context)

def userDetails(request, username):
    """
    User details
    """
    user_details = get_object_or_404(UserProfile, username=username)

    template = 'usermanager/user_details.html'
    context = {
        'user_details': user_details,
    }
    return render(request, template, context)

urls.py

path('user-list/', views.usersList, name='users'),
path('<username>/', views.userDetails, name='user_details'),

users_list.html

  {% for user in users %}
  <h1>{{ user.username }}</h1>
  <p>Name: {{ user.first_name }}</p>
  <p>Surname: {{ user.last_name }}</p>
  <p>Email: {{ user.email }}</p>
  {% if request.user.is_superuser %}
    {% if user.is_staff %}
    <button class="btn btn-warning">Staff</button>
    {% else %}
    <button class="btn btn-primary">User</button>
    {% endif %}
  {% endif %}
  <a class="btn btn-danger" href="{% url 'user_details' username=user.username %}">Details</a>
  <hr>
  {% endfor %}

user_details.html

{% if request.user == user %}
<h1>Your profile</h1>
{% else %}
<h1>Profile of {{ user.username }}</h1>
{% endif %}
<hr>
<div class="container">
  <p>Name: {{ user.first_name }}</p>
  <p>Surname: {{ user.last_name }}</p>
  <p>Email: {{ user.email }}</p>
  {% if request.user.is_superuser %}
    {% if user.is_staff %}
    <button class="btn btn-warning">Staff</button>
    {% else %}
    <button class="btn btn-primary">User</button>
    {% endif %}
  {% endif %}
  <p>Joined: {{ user.date_joined }}</p>
  <p>Last login: {{ user.last_login }}</p>
  {% if user.is_active %}
  <button class="btn btn-success">Active</button>
  {% else %}
  <button class="btn btn-secondary">Inactive</button>
  {% endif %}
</div>

I can see correctly the list of all users. Bob is one of my user, if I click on his details I see my personal profile. This happen for all of users details, I see always my profile. I'm logged in as superuser.

What I've wrong?

Upvotes: 0

Views: 86

Answers (2)

Pol Frances
Pol Frances

Reputation: 402

Your object for the user_Details is {{user_details}}, so you should be using that as a variable on the template:

{% if request.user == user_details %} and so on. That should solve your problem.

But as explained here in the docs, the template variable user is already used by Django to let you see the actual user information.

In your user list template you don't have this problem because you are overriding the user variable with the for.

Another recommendation is: Evade overriding the user variable on a template. for example on your users_list:

{% for usr in users %}
    <h1>{{ usr.username }}</h1>
    <p>Name: {{ usr.first_name }}</p>
    <p>Surname: {{ usr.last_name }}</p>
    <p>Email: {{ usr.email }}</p>
    {% if request.user.is_superuser %}
      {% if usr.is_staff %}
      <button class="btn btn-warning">Staff</button>
      {% else %}
      <button class="btn btn-primary">User</button>
      {% endif %}
    {% endif %}
    <a class="btn btn-danger" href="{% url 'user_details' username=usr.username %}">Details</a>
    <hr>
    {% endfor %}

Upvotes: 1

MaxDragonheart
MaxDragonheart

Reputation: 1290

It is my stupid error... The contex of userDetails is user_details but I use user inside the template

Upvotes: 0

Related Questions