user2170928
user2170928

Reputation: 247

django request.user.is_superuser doesn't work

I want to show a button if the user is a superuser. I've found differents examples but my code doesn't work. The button doesn't appear. Anybody knows 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 only have a user in the database and he is a super user. I can see the "logout" button and the other one, but not the "crear zona" button.

Upvotes: 19

Views: 30114

Answers (1)

Leandro
Leandro

Reputation: 2247

You want this Generic view:

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

are the context processors in settings?

This is more redeable:

{% 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've changed {% if request.user.is_superuser %} to {% if user.is_superuser %}

Upvotes: 17

Related Questions