Skip to main content
 首页 » 编程设计

python之Django 根据用户组成员身份在模板中显示项目

2025年04月02日41bonelee

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

View .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) 

模型.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 %} 

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

请您参考如下方法:

我已经设法解决了!它需要使用我必须学习的 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) 

我希望这对以后的人有所帮助。