How do I link different users to view the page differently?

hi if anyone is familiar with django, i'm working on how different group of users will be able to view different buttons, but i cant seem to make that work; my elif doesn't work, when i'm logged in to employee i'm able to view the buttons but when im logged in the inventory manager i can only view undefined user

employee view inventory manager view

below are my codes views.py

def menu(request):
    user = request.user
    is_employee = request.user.groups.filter(name='employee').exists()
    is_inventorymanager = user.groups.filter(name='inventorymanager').exists()
    is_financialdirector = user.groups.filter(name='financialdirector').exists()
    is_vendor = user.groups.filter(name='vendor').exists()
    return render(request, 'app/menu.html', {
        'is_employee': is_employee,
        'is_inventorymanager': is_inventorymanager,
        'is_financialdirector': is_financialdirector,
        'is_vendor': is_vendor
    })

menu.html (interface)

{% extends "app/layout.html" %}

{% block content %}

<div>
    <br />

    {% if is_employee %}
         <h2>Employee Menu</h2>

    {% elif is_inventorymanager %}
         <h2>Inventory Manager Menu</h2>

    {% elif is_financialdirector %}
         <h2>Financial Director Menu</h2>

    {% elif is_vendor %}
         <h2>Vendor Menu</h2> 

    {% else %} 
        <h2>Undefined</h2>    

    {% endif %}
    <br />


    <table>
    {% if is_employee %}

        <tr><td>
    <form class="margintop" action='createpurchaseorder' method='GET'>
        <button type="submit" class="btn btn-info"> EMP | Create Purchase Order</button><br />  
    </form>
        </td></tr>

        <tr><td>
    <form class="margintop" action='createdeliveryorder' method='GET'>
        <button type="submit" class="btn btn-info"> VEN | Create Delivery Order</button><br />
    </form>
        </td></tr>

        <tr><td>
    <form class="margintop" action='listview_po' method='GET'>
        <button type="submit" class="btn btn-info">EMP FM VEND | View Purchase Orders</button><br />
    </form>
        </td></tr>

        <tr><td>
    <form class="margintop" action='listview_do' method='GET'>
            <button type="submit" class="btn btn-info">IM VEN | View Delivery Orders </button><br />
    </form>
        </td></tr>

        <tr><td>
    <form class="margintop" action='list_po' method='GET'>
        <button type="submit" class="btn btn-info">FD | Verify Purchase Orders</button><br />
    </form>
        </td></tr>

        <tr><td>
    <form class="margintop" action='list_do' method='GET'>
        <button type="submit" class="btn btn-info">IM | Verify Delivery Orders</button><br />
    </form>
        </td></tr>


        <tr><td>
    <form class="margintop" action='viewpurchaseorder' method='GET'>
        <button type="submit" class="btn btn-outline-info waves-effect">(Bug) Search Purchase Order</button><br />
    </form>
        </td></tr>

        
    {% elif is_inventorymanager %}            
        <tr><td>
    <form class="margintop" action='verifydeliveryorder' method='GET'>
        <button type="submit" class="btn btn-info">Verify Delivery Order</button><br />
    </form>
        </td></tr>
            
        <tr><td>
    <form class="margintop" action='viewdeliveryorder' method='GET'>
        <button type="submit" class="btn btn-outline-info waves-effect">View Delivery Order</button><br />
    </form>
        </td></tr>    
   
        <tr><td>
    <form class="margintop" action='createpurchaseorder' method='GET'>
        <button type="submit" class="btn btn-info">Create Purchase Order</button><br />  
    </form>
        </td></tr>        
        
    {% elif is_financialdirector %}
        <tr><td>
    <form class="margintop" action='verifypurchaseorder' method='GET'>
        <button type="submit" class="btn btn-info">Verify Purchase Order</button><br />
    </form>
        </td></tr>
            
        <tr><td>
    <form class="margintop" action='viewpurchaseorder' method='GET'>
        <button type="submit" class="btn btn-outline-info waves-effect">View Purchase Order</button><br />  
    </form>
        </td></tr>

    {% elif is_vendor %}            
        <tr><td>
    <form class="margintop" action='viewpurchaseorder' method='GET'>
        <button type="submit" class="btn btn-outline-info waves-effect">View Purchase Order</button><br />
    </form>
        </td></tr>
            
        <tr><td>
    <form class="margintop" action='viewdeliveryorder' method='GET'>
        <button type="submit" class="btn btn-outline-info waves-effect">View Delivery Order</button><br />
    </form>
        </td></tr>       

    {% else %}            

        <tr><td>
            <p>Undefined User.</p>
        </td></tr>
        <tr><td>
            
    {% endif %}            
    </table>
</div>

{% endblock %}

looks like you have an option to make user a member of different groups. it means if user is employee and inventory manager - thats fine

If it suppose to be like that:

then you will display different menus in one template

IF it's not:

I suggest to you, use the another field for user, something like:

class UserRoles(models.IntegerChoices):
    EMPLOYEE = 1, "Employee"
    INVENTORY_MANAGER = 2, "Inventory manager"
    ...

class CustomUser(AbstractUser):
    ...

    role = models.PositiveSmallIntegerField(choices=UserRoles.choices, default=UserRoles.employee)
    ...

then you can define the const with templates:


MENU_TEMPLATE_PER_USER_ROLE = {
    UserRoles.EMPLOYEE: "templates/employee_menu.html",
    UserRoles.INVENTORY_MANAGER: "templates/inventory_manager_menu.html",
    ...
}

and use this in views when u choose the templates

def menu(request):
    template = MENU_TEMPLATE_PER_USER_ROLE.get(request.user.role, default="templates/default_menu.html")
    return render(request, template)
Back to Top