Acess dictionary value in template
I have a list view where I define queryset as a list of objects if current user is not a top manager otherwise queryset is a dictionary with key being employee and value being list of objects. So I want to know how to display the key from this dict and then list it's value - a list. I tried to do it like that
<ul>
{% for k, v in employees_list.items %}
<li><a href="{% url 'employees:employees-detail' pk=v.pk %}">
{{ v.title }} {{ v.last_name }} {{ v.first_name }}</a></li>
{% endfor %}
</ul>
It didn't work out. Here's the view
class EmployeesListView(LoginRequiredMixin, ListView):
model = Employees
template_name = 'employees/employees_list.html'
context_object_name = 'employees_list'
fields = ['last_name', 'first_name', 'title', 'birth_date', 'reports_to']
is_top_manager = False
def get_queryset(self):
#current_emp = get_current_users_employee(self.request.user)
#return super().get_queryset().filter(reports_to=current_emp)
current_emp = get_current_users_employee(self.request.user)
this_employees_subordinates = Employees.objects.filter(reports_to=current_emp)
if str(current_emp.title) != "Top manager":
return this_employees_subordinates
else:
self.is_top_manager = True
print("this user is top manager")
lower_level_subordinates = {}
for subordinate in this_employees_subordinates:
lower_level_subordinates[subordinate] = Employees.objects.select_related('title').filter(reports_to=subordinate)
print(lower_level_subordinates)
return lower_level_subordinates