【问题标题】:Django display items in template based on user group membershipDjango根据用户组成员在模板中显示项目
【发布时间】:2016-10-25 19:33:31
【问题描述】:

我正在开发内部 Intranet 的应用程序门户页面。该站点使用 django-auth-ldap 与 Active Directory 集成,并且应该仅根据用户所属的组显示链接项目。作为我的模型的一部分,有一个“required_group”字段,其中包含显示每个链接所需的组的名称。但是,我正在努力遍历链接并根据用户组成员身份过滤列表。我希望这是有道理的!这是一些代码:

views.py

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)

models.py

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')

index.html

{% extends 'webapps/base.html' %}
{% block content %}
<div class="container">
    <div class="row">
        <div class="col-sm-12 text-center">
            <h2>Hospital Web Applications</h2>
        </div>
    </div>
</div>


<div class="container">
  <div class="row text-center">
          {% for link in links_to_display %}
              <div class="col-md-2 col-xs-2 link-div"><a href="{{ link.link_target }}" target="_blank"><img src="/media/{{ link.image_file }}"><br />{{ link.display_name }}</a></div>
          {% endfor %}

  </div>
    <hr>
</div>
{% endblock %}

非常感谢您提供的任何帮助。

【问题讨论】:

    标签: python django


    【解决方案1】:

    我已经解决了!它需要使用我必须了解的 Q 对象。这是我的views.py,显示了最终的 Q 对象查询。

    from django.shortcuts import render, redirect
    from django.http import HttpResponse
    from django.contrib.auth import authenticate, login, logout
    from django.db.models import Q
    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" or "r-webapps-all" 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 which the user has access to.
            else:
                all_user_groups = request.user.groups.all()
                q_objects = Q()
                for group in all_user_groups:
                    q_objects |= Q(required_group__contains=group)
    
                links_to_display = Link.objects.filter(q_objects)
    
                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)
    

    我希望这对将来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 2021-08-16
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多