使用Django在HTML模板中对组权限

时间:2017-01-26 13:14:24

标签: html django

我的项目遇到了一些问题。除了一个(Superadmin)之外,我会授权从Django website到所有组(访客,客户......)的一部分。

这是我的 HTML模板

        <!-- Home tab -->

        <ul class="nav navbar-nav">
            <li><a href="{% url "accueil" %}"> <span class="glyphicon glyphicon-home"></span> Accueil </a></li>

            {% if request.user|has_group:"admin" %}

            <li class = "dropdown">
                <a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
                    Informations Mairie
                <b class = "caret"></b>
                </a>
                <ul class = "dropdown-menu">
                    <li><a href = "{% url "Mairieform" %}"> <span class="glyphicon glyphicon-pencil"></span> Créer/Editer les informations de la Mairie </a></li>
                    <li><a href = "{% url "Mairieresume" %}"> <span class="glyphicon glyphicon-home"></span> Consulter les informations de la Mairie </a></li>
                </ul>
            </li>

            <li class = "dropdown">
                <a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
                    Actes Etat Civil
                <b class = "caret"></b>
                </a>
                <ul class = "dropdown-menu">
                    <li><a href = "{% url "home" %}"> <span class="glyphicon glyphicon-user"></span> Fiches Individuelles </a></li>
                    <li><a href = "{% url "BChome" %}"> <span class="glyphicon glyphicon-baby-formula"></span> Actes de Naissance </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-heart"></span> Actes de Mariage </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-fire"></span> Actes de Divorce </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-alert"></span> Actes de Décès </a></li>
                </ul>
            </li>
        </ul>

            {% elif request.user |has_group:"visiteur","employé", "officier", "maire" %}

        <ul class="nav navbar-nav">
            <li><a href="{% url "accueil" %}"> <span class="glyphicon glyphicon-home"></span> Accueil </a></li>
            <li class = "dropdown">
                <a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
                    Informations Mairie
                <b class = "caret"></b>
                </a>
                <ul class = "dropdown-menu">
                    <li><a href = "{% url "Mairieresume" %}"> <span class="glyphicon glyphicon-home"></span> Consulter les informations de la Mairie </a></li>
                </ul>
            </li>

            <li class = "dropdown">
                <a href = "#" class = "dropdown-toggle" data-toggle = "dropdown">
                    Actes Etat Civil
                <b class = "caret"></b>
                </a>
                <ul class = "dropdown-menu">
                    <li><a href = "{% url "home" %}"> <span class="glyphicon glyphicon-user"></span> Fiches Individuelles </a></li>
                    <li><a href = "{% url "BChome" %}"> <span class="glyphicon glyphicon-baby-formula"></span> Actes de Naissance </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-heart"></span> Actes de Mariage </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-fire"></span> Actes de Divorce </a></li>
                    <li><a href = "{% url "BCnotfound" %}"> <span class="glyphicon glyphicon-alert"></span> Actes de Décès </a></li>
                </ul>
            </li>
        </ul>

            {% endif %}

正如您所看到的,我只有一个选项卡,除了Superadmin组之外,所有组都必须是隐藏的。它适用于Superadmin,但我的问题是:

我如何写这一行:{% if request.user|has_group:"admin" %} for:

  • 除Superadmin以外的所有团体
  • 或访客,客户......

这样的事情:

{% elif request.user |has_group:"visiteur","employé", "officier", "maire" %}

谢谢!

编辑:

这是我的应用程序之一的views.py文件:

#-*- coding: utf-8 -*-

import requests, os, json, glob
from django.shortcuts import render, reverse, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse
from .models import BirthCertificate, Country
from .forms import BirthCertificateForm
from django.db import connection
from django.template import Context
from django.template.loader import get_template
from xhtml2pdf import pisa

import time, random

@login_required
def BirthCertificate_Home(request) :

    return render(request, 'BC_accueil.html')

@login_required
def BirthCertificate_notfound(request) :

    return render(request, 'Not_Found.html')

@login_required
def BirthCertificate_accueil(request) :

    return render(request, 'Accueil.html')

@login_required
def BirthCertificate_Form(request) :
    # Fonction permettant de créer le formulaire Acte de Naissance et le remplissage

    Bform = BirthCertificateForm(request.POST or None)
    template_name = 'BC_form.html'

    if Bform.is_valid() :   # Vérification sur la validité des données
        if '_preview2' in request.POST :
            post = Bform.save(commit=False)
            template_name = 'BC_preview.html'

        elif '_save2' in request.POST :
            post = Bform.save()
            return HttpResponseRedirect(reverse('BC_treated', kwargs={'id': post.id}))

    return render(request, template_name, {"Bform" : Bform})

@login_required
def BirthCertificate_Resume(request, id) : ...

@login_required
def BirthCertificate_PDF(request, id) : ...

1 个答案:

答案 0 :(得分:0)

最好在视图中执行此操作。

class MyView(ViewClass):
    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        if self.request.user.is_superuser:
            context['show_tab'] = True
            return context
        return context

然后你可以访问这是模板

{% if show_tab %}
  <myHtml/>
{% endif %}

如果您使用的是基于类的视图,我上面的答案就是。如果您使用基于功能的视图,这可能看起来更熟悉。

def my_view(request):
    context = {}
    if request.user.is_superuser<group logic>:
         context['show_tab'] = True
         return context
    return render(request, 'mytemplate.html', context)

这里的一点是,您可以将字典(如上下文)传递到视图中,以便在模板中将其提供给您。