Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

279
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda