我的項目出現了一些問題。我會授權從我的Django website
到除了一個(Superadmin)以外的所有組(訪客,客戶...)。使用Django的HTML模板中的組權限
這是我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 %}
正如你所看到的,我只是有這不得不成爲除超級管理員組的所有羣體無形的一個標籤。它適用於超級管理員,但我的問題是:
我怎麼可以這樣寫行:{% if request.user|has_group:"admin" %}
爲:
- 除了超級管理員
- 或訪問者,客戶,所有組...
事情是這樣的:
{% 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) : ...
[如何檢查(在模板中)用戶是否屬於組](http://stackoverflow.com/questions/34571880/how-to-check-in-template-whether-user-belongs-to -group) –