title: Django display items in template based on user group membership
body: <p>I am working on an application portal page for an internal intranet. The site is integrated with Active Directory using django-auth-ldap and should only display link items based on which groups a user is a member of. As part of my model there is a 'required_group' field which contains the name of the group needed to display each link. However, I am struggling to loop through the links and filter the list depending on the users group membership. I hope that makes sense! Here is some code:</p>
  
  <p>views.py</p>
  
  <pre><code>from django.shortcuts import render, redirect
  from django.http import HttpResponse
  from django.contrib.auth import authenticate, login, logout
  from .models import Link
  
  
  def index(request):
      # Check that user is authenticated
      if request.user.is_authenticated():
          # If user is authenticated and a member of "Domain Admins" show all of the links
          if request.user.groups.filter(name="Domain Admins").exists() or request.user.groups.filter(name="r-webapps-all").exists():
              links_to_display = Link.objects.all()
              context = {
                  'links_to_display': links_to_display,
              }
          # Else loop through all links and only display links where link.required_group is in request.user.groups.all()
          # This is where I am stuck!
          else:
              links_to_display = Link.objects.all()
              for link in links_to_display:
                  if request.user.groups.filter(name=link.required_group):
                      links_to_display = links_to_display.filter(required_group=link.required_group)
  
              context = {
                  'links_to_display': links_to_display,
              }
      # If user is not authenticated only show links which have "Domain Users" as the required group
      else:
          links_to_display = Link.objects.filter(required_group="Domain Users")
          context = {
              'links_to_display': links_to_display,
          }
          # Login form POST
          if request.method == 'POST':
             username = request.POST['username']
             password = request.POST['password']
             user = authenticate(username=username, password=password)
             if user is not None:
                  if user.is_active:
                      login(request, user)
                  return redirect('/webapps/')
             else:
                  return HttpResponse('ERROR')
      # Render web page
      return render(request, 'webapps/index.html', context)
  </code></pre>
  
  <p>models.py</p>
  
  <pre><code>class Link(models.Model):
      class Meta:
          ordering = ['display_name']
      link_target = models.CharField(max_length=200)
      display_name = models.CharField(max_length=200)
      required_group = models.CharField(max_length=200)
      image_file = models.FileField(upload_to='webapps')
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>{% extends 'webapps/base.html' %}
  {% block content %}
  &lt;div class="container"&gt;
      &lt;div class="row"&gt;
          &lt;div class="col-sm-12 text-center"&gt;
              &lt;h2&gt;Hospital Web Applications&lt;/h2&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  
  &lt;div class="container"&gt;
    &lt;div class="row text-center"&gt;
            {% for link in links_to_display %}
                &lt;div class="col-md-2 col-xs-2 link-div"&gt;&lt;a href="{{ link.link_target }}" target="_blank"&gt;&lt;img src="/media/{{ link.image_file }}"&gt;&lt;br /&gt;{{ link.display_name }}&lt;/a&gt;&lt;/div&gt;
            {% endfor %}
  
    &lt;/div&gt;
      &lt;hr&gt;
  &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>Many thanks in advance for any help you can provide.</p>