Reputation: 1792
I want the "Admin" link to display based on a field in my UserLevels
model called "access_level". If the currently logged in user's access_level field is equal to "admin" then the "Admin" link should display.
Currently there are no errors however the "Admin" link will display for every logged in user and not take into account the access_level field value.
HTML:
{% if user.is_authenticated and userlevels.access_level == 'admin' %}
<a class="nav-link" href="{% url 'adminpanel:widgets' %}">Admin</a>
{% endif %}
Adminpanel app models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class UserLevels(models.Model):
user = models.OneToOneField(User)
access_level = models.CharField(max_length=5,default='user',blank=True)
def __str__(self):
return self.user.username
Upvotes: 0
Views: 273
Reputation: 4213
should be:
{% if request.user.is_authenticated and request.user.userlevels.access_level == 'admin' %}
<a class="nav-link" href="{% url 'adminpanel:widgets' %}">Admin</a>
{% endif %}
but is better if you pass that in your view and not in the template
Upvotes: 2
Reputation: 5594
userlevel
is not valid on its own. It is a property of user, which is set by the fact that you created a oneToOne
field.
Try
{% if user.is_authenticated and user.userlevels.access_level == 'admin' %}
<a class="nav-link" href="{% url 'adminpanel:widgets' %}">Admin</a>
{% endif %}
Upvotes: 1