javascript - Django为不同的组模板不同的项目。用户身份验证和组隔离
问题描述
因此,我正在尝试制作一个像网站一样的教室,但这仅适用于我认识的人正在运行的课程。我希望不同的群体对网站有不同的看法。例如组“Guest”看不到菜单项组“Student”可以看到。但它不是很有效......这是我的一些菜单代码:
{% 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%}
但上面也引发了一个错误。
TemplateSyntaxError at /assignments
Unexpected end of expression in if tag.
以下是有关该错误的更多信息:
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
解决方案
这是如何做到的。我不知道为什么这行得通,而上面的那个不行,但它就是这样。但我认为你必须指定每个条件而不是使用 else
代码:
{% 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%}
推荐阅读
- laravel - 通过 Vapor 部署时的 Laravel API 限制问题
- asp.net-mvc - 如何正确显示视图 ASP.NET?
- angular - Angular 在测试中操纵服务值
- nginx - nginx 配置 / 文件夹从与 / 不同的路径运行
- prolog - 如何在 Prolog 中编写特定游戏?
- java - 如何重构这个 REST API Spring Boot 应用程序?
- python - 下期权订单时出现错误 201 交互式经纪人 tws
- python - 如何在 Python 中编写 eof?
- visual-studio - 在 Visual Studio 2019 上禁用建议列表自动调整大小?
- python - 如何在Django中通过views.py将多个值从models.py传递到HTML