Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

Use an if-clause to determine which role the logged-in user has:

{% if user.role == "admin" %}
    {% include 'admin_sidebar.html'%}
{% elif user.role == "user" %}
    {% include 'user_sidebar.html'%}
{% endif %}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading