skip to Main Content

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>

2

Answers


  1. 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 %}
    
    Login or Signup to reply.
  2. Pass a variable from Django to the Jinja2 templating which tells you if you want admin or not and then use the conditional statements within Jinja2:

    {% if admin %}
       {% include 'admin_sidebar.html'%}
    {% else %}
       {% include 'user_sidebar.html'%}
    {% endif %}
    

    Then in the python:

    return render(request, 'template.html', {'admin': True})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search