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
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%}