Sam
Sam

Reputation: 19

{% request.user ==" "%} Checking User and rendering html element with if else Django Templatetag

Html code:

            {% if request.user== "Sangam" %}
                <li class=""> <a class="text-left " href="landscapes.html" role="button" aria-haspopup="true"
                    aria-expanded="false"><i class="flaticon-bar-chart-1"></i> Landscape </a>
                </li>
             {% endif %} 

             {% if request.user  == "Sam" %}
            <li class=""> <a class="text-left " href="characters.html" role="button" aria-haspopup="true"
                aria-expanded="false"><i class="flaticon-bar-chart-1"></i> Characters </a>
            </li>
             {% endif %} 

I want to do is if the user is Sangam then render a certain html element for him only. But,It doesn't render so. Can I do this?.Also,I wonder how can we render certain html elements to users that we have list him/her to a certain Group in Django-admin panel

Upvotes: 1

Views: 280

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476537

You need to compare the .username of the logged in user:

{% if request.user.username == "Sangam" %}
    <li class=""> <a class="text-left " href="landscapes.html" role="button" aria-haspopup="true" aria-expanded="false"><i class="flaticon-bar-chart-1"></i> Landscape </a></li>
{% endif %}

{% if request.user.username == "Sam" %}
    <li class=""><a class="text-left " href="characters.html" role="button" aria-haspopup="true" aria-expanded="false"><i class="flaticon-bar-chart-1"></i> Characters </a></li>
{% endif %}

Upvotes: 2

Related Questions