How to add a template using {% include %} tag, by comparing which user is logged in, on django?

My asset_list html is like this

<div class="right">
        <div class="search-container">
            <input type="text" id="searchInput" placeholder="Search by AssetName..." aria-label="Search">
            
                <button class="add-button" aria-label="Add Asset" id="addUserButton"><i class="fas fa-plus"></i> Add</button>
            
        </div>

login database is like this , with roles admin and user

class UserDetails(models.Model):
    username = models.CharField(max_length=100, unique=True)
    password = models.CharField(max_length=100)  # Ideally, this should be hashed
    role = models.CharField(max_length=45)

views for asset list

def asset_list(request):
    users = Asset_Table.objects.all()
    return render(request, 'asset_mng/asset_pro.html', {'users': users})

I have 2 roles Admin and User , when Admin is logged in, I want to show admin side bar and if user is logged in , show user sidebar.

{% include 'admin_sidebar.html'%} or {% include 'user_sidebar.html'%}

<div class="right">
        <div class="search-container">
            <input type="text" id="searchInput" placeholder="Search by AssetName..." aria-label="Search">
            
                <button class="add-button" aria-label="Add Asset" id="addUserButton"><i class="fas fa-plus"></i> Add</button>
            
        </div>

It looks like you are looking for the request.user attribute.

{% if request.user.is_authenticated %}
# login user process
{% else %}
# not login user process
{% endif %}
1 Like