This is what I expect: if a user has position "1" (Admin) the nav menu in home.html must show "Admin" link to an admin page. If a user has position "2" (User) it won't show the link.
But when I run server this code generate as many Admin links as there are registered users. I want only one link for currently logged-in user to be shown. How can I do that? I know that there's something wrong with "for user in position", but how to fix it for currently logged-in user?
models.py
class Profile(models.Model):
positions = (
('1', 'Admin'),
('2', 'User'),
)
user = models.OneToOneField(User, on_delete=models.CASCADE, unique=True)
image = models.ImageField(default='default.jpg', upload_to='profile_pics')
position = models.CharField(max_length=50, default='Position', choices=positions)
views.py
def home(request):
user_position = Profile.objects.all()
return render(request, 'home.html', {
'position': user_position,
})
home.html
{% for user in position %}
{% if user.position == '1' %}
<a class="nav-link" aria-current="page" href="/admin">Admin</a>
{% endif %}
{% endfor %}