Kaïss Bouali
Kaïss Bouali

Reputation: 83

How to display something in a django template according to which user is logged in?

I am displaying a table in a django template, and I have 2 identical templates but with 2 different tables. I have 2 different user groups. I want to display a different template according to which auth group the user belongs to. For example:

if user is in group A: render template1.html else if user is in group B: render template2.html

All I know right now is that I used the @login_required decorator, so the view is not gonna be shown if the user is not logged in. But this includes all users, and is not specific to groups.

def home(request):
    ecv_count = Dossier.objects.filter(status_admin='ECV').count()
    v_count = Dossier.objects.filter(status_admin='V').count()
    r_count = Dossier.objects.filter(status_admin='R').count()
    c_count = Dossier.objects.filter(status_admin='C').count()
    context = {
        'dossiers': Dossier.objects.all(),
        'ecv_count': ecv_count,
        'v_count': v_count,
        'r_count': r_count,
        'c_count': c_count
    }

    return render(request, 'dashboard/home.html', context)

I want to have the view check the user group, and render a different template with a different context.

Upvotes: 1

Views: 86

Answers (1)

Arjun Shahi
Arjun Shahi

Reputation: 7330

You can check like this:

def home(request):
    if request.user.groups.filter(name='some_group').exists():
        # do something ....
    else:
        #do somethig...

Upvotes: 2

Related Questions