Home > Software design >  Python Django {% if usuario.username == user %} not work :(
Python Django {% if usuario.username == user %} not work :(

Time:09-23

I need some help. I want to list all user avoid the login user.

if I print user= juan if I print usuario.username=juan There have the same string

{% if usuario.username == user %}
 Nothing
{% else %}
<tr><td>
{{usuario.username}}
<img src="{% static 'assets/img/icono_writeMSJ.png' %}"     height="30px">
</td>
</tr>
{% endif %}

if i change for example

{% if usuario.username == 'juan' %}
 Nothing
{% else %}
<tr><td>
{{usuario.username}}
<img src="{% static 'assets/img/icono_writeMSJ.png' %}"     height="30px">
</td>
</tr>
{% endif %}

this work fine, why?? :(

CodePudding user response:

user is an object. Stringify it. That should work

{% if usuario.username == user|stringformat:"s" %}

CodePudding user response:

======== excluded current logged-in user ========

========= in HTML ==============

<h3>All Users list</h3>

{% if  user.is_authenticated  %}
     <ul>
          {% for i in all_users %}
               <li>{{i.username}}</li>
          {% endfor %}
     </ul>
{% else %}
     <h4>Please login first</h4>


======= in views.py ===========

def HomeView(request):
    all_users = User.objects.all().exclude(id=request.user.id).values('username')
    return render(request,'index.html',{'all_users':all_users})
  • Related