Django request.user.is_superuser not working

I want to show the button if the user is superuser. I found some examples, but my code is not working. The button is not displayed. Does anyone know why?

views.py

def inici(request): zones = Zona.objects.all() return render_to_response('principal/inici.html', dict(zones=zones), context_instance = RequestContext(request)) 

inici.html

 {% if not user.is_authenticated %} .... {% else %} <ul> <li class="nivell1"> <a href="/accounts/logout/?next=/">Logout</a> </li> <li class="nivell1"> <a class="nivell1" herf="#"> Configuració </a> </li> {% if request.user.is_superuser %} <li class="nivell1"> <a href="zona/crear/">Crear zona</a> </li> {% endif %} </ul> {% endif %} 

I have only the user in the database and he is superuser. I see the "Exit" button and another, but not the "crear zona" button.

+6
source share
1 answer

You want this general view :

 class IniciView(ListView): template_name = 'principal/inici.html' model = Zona 

- are context processors in the settings?

You can reinstall this more:

 {% if user.is_authenticated %} <ul> <li class="nivell1"> <a href="/accounts/logout/?next=/">Logout</a> </li> <li class="nivell1"> <a class="nivell1" herf="#"> Configuració </a> </li> {% if user.is_superuser %} <li class="nivell1"> <a href="zona/crear/">Crear zona</a> </li> {% endif %} </ul> {% else %} ... {% endif %} 

I changed {% if request.user.is_superuser %} to {% if user.is_superuser %}

+6
source

Source: https://habr.com/ru/post/948780/