Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

277
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!