【问题标题】:Django template different items for different groups. User authentication and group segregationsDjango为不同的组模板不同的项目。用户身份验证和组隔离
【发布时间】:2021-12-25 18:00:31
【问题描述】:

所以我正在尝试制作一个像网站一样的教室,但这仅适用于我认识的人正在运行的课程。我希望不同的群体对网站有不同的看法。例如组“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

【问题讨论】:

    标签: javascript python html django django-templates


    【解决方案1】:

    这里是如何做到这一点。我不知道为什么这行得通,而上面的那个不行,但它就是这样。但我认为你必须指定每个条件而不是使用 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%}
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-31
      • 2020-02-13
      • 1970-01-01
      • 2018-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      相关资源
      最近更新 更多