Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

278
Visualizações
Django template different items for different groups. User authentication and group segregations

So I am trying to make a classroom like website but it is only for a course someone I know is running. I want different groups to have a different view for the website. For example group "Guest" can't see menu items group "Student" can see. But it is not quite working... Here's a bit of my code for the menu:

      {% if user.is_authenticated %}

      {% for group in user.groups.all %}

      {% ifequal group.name 'Student' %}

      <div class="menu">
          <button onclick="location.href='http://10.0.0.60:8000/';" class="home en">Home</button>

        <button onclick="location.href='http://10.0.0.60:8000/news';" class="news en">News</button>

        <button onclick="location.href='http://10.0.0.60:8000/about';" class="about en">About us</button>

          <button onclick="location.href='http://10.0.0.60:8000/lessons';" class="home en">Lessons</button>

          <button onclick="location.href='http://10.0.0.60:8000/assignments';" class="home en">Assignments</button>

      </div>

      {% endifequal %}

      {%ifequal group.name 'Guest'%}

        <button onclick="location.href='http://10.0.0.60:8000/';" class="home en">Home</button>

        <button onclick="location.href='http://10.0.0.60:8000/news';" class="news en">News</button>

        <button onclick="location.href='http://10.0.0.60:8000/about';" class="about en">About us</button>

      {%endifequal%}

      {%endfor%}

      {%else%}

      <div class="menu">
          <button onclick="location.href='http://10.0.0.60:8000/';" class="home en">Home</button>

        <button onclick="location.href='http://10.0.0.60:8000/news';" class="news en">News</button>

        <button onclick="location.href='http://10.0.0.60:8000/about';" class="about en">About us</button>
      </div>

      {%endif%}

But the above also raises an error.

TemplateSyntaxError at /assignments
Unexpected end of expression in if tag.

Here's some more info about the error:

Request Method: GET
Request URL:    http://10.0.0.60:8000/assignments
Django Version: 3.2.7
Exception Type: TemplateSyntaxError
Exception Value:    
Unexpected end of expression in if tag.
Exception Location: /home/*****/.local/lib/python3.9/site-packages/django/template/smartif.py, line 144, in nud
Python Executable:  /usr/bin/python3
Python Version: 3.9.5
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Here's how to do it. I don't know why this works and the one above doesn't , but it is what it is. But I think you have to specify each and EVERY condition instead of using else

Code:

{% if user.is_authenticated %}

      {% for group in user.groups.all %}

      {% ifequal group.name 'Student' %}

      <div class="menu">
          "items for group student"

      </div>

      {% endifequal %}

      {%ifequal group.name 'Guest'%}

        <div class="menu">
          "items for group Guest"
      </div>

      {%endifequal%}

      {%endfor%}

      {%elif not user.is_authenticated%}

      <div class="menu">
          "items for anonymous users"
      </div>

      {%endif%}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda