2017-01-26 47 views
0

我的項目出現了一些問題。我會授權從我的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) : ... 
+0

[如何檢查(在模板中)用戶是否屬於組](http://stackoverflow.com/questions/3457188​​0/how-to-check-in-template-whether-user-belongs-to -group) –

回答

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) 

的一點是,你可以通過一本字典(如上下文)到視圖中有它的模板提供給您。

+0

我必須在admin.py中編寫'MyAdminView'?我會嘗試你的榜樣。謝謝 – Andro

+0

對不起,我訪問'請求'不正確。更新了我的答案。 –

+0

我必須在哪裏寫出答案的第一部分?在view.py或admin.py? – Andro