简体   繁体   中英

How do I display a Django Template based on the User's Group?

First post and am fairly new to django/python. I am getting multiple errors when following a similar thread of how to show different pages for users in different django groups. I created my own context processor in my view but I am getting the error code that the "local_admin variable is not defined.

View:

def user_context(request):
  if request.user.is_authenticated():
    is_admin = is_local_admin(request.user)
  else:
    is_admin = False

return {
    'is_local_admin': is_admin
}

Template HTML:

 {% if is_admin %}
 {% include 'partials/sidebar.html' %}
 {% else %}
 {% include 'partials/sidebar2.html' %}
 {% endif %}

Any suggestions on what I am doing wrong? Thanks.

The problem is that your variable in the template is called 'is_local_admin', so call it different, like:

return {
    'is_admin': is_admin
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM