Hi there once more,
today I don’t need help to achieve a goal, my code already works…but I can’t help thinking about, that it is not the pythonic way and not the “DoNotRepeatYourself” Way of Django to do it like this…but it was the first solution that came to my mind. However, I am not very happy about. What I want to do, is according to some law regulations display a page with a summary of Users/Roles/Rights.
I managed to do that with the following code in views.py:
def rights(request):
context = {
'groups': Group.objects.all(),
'admin': User.objects.filter(groups__name='admin').order_by('last_name'),
'examiner': User.objects.filter(groups__name='examiner').order_by('last_name'),
'manv': User.objects.filter(groups__name='manv').order_by('last_name'),
'office': User.objects.filter(groups__name='office').order_by('last_name'),
'view_all': User.objects.filter(groups__name='view_all').order_by('last_name'),
'view_examinations': User.objects.filter(groups__name='view_examinations').order_by('last_name'),
'view_retired': User.objects.filter(groups__name='view_retired').order_by('last_name')
}
return render(request, 'rights.html', context)
and this code in template:
{% for group in groups %}
<div class="col">
<div class="card h-auto border-dark border-2 bg-light">
<div class="card-header text-center fs-4 text-white bg-primary bg-gradient"><b>{{ group.name }}</b></div>
<div class="card-text m-1 ps-1">
{% if group.name == 'admin' %}
{% for user in admin %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'examiner' %}
{% for user in examiner %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'manv' %}
{% for user in manv %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'office' %}
{% for user in office %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'view_all' %}
{% for user in view_all %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'view_examinations' %}
{% for user in view_examinations %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
{% if group.name == 'view_retired' %}
{% for user in view_retired %}
<nobr>{{ user.username }} - {{ user.last_name }}, {{ user.first_name|slice:1 }}.</nobr><br>
{% endfor %}
{% endif %}
</div>
</div>
</div>
{% endif %}
{% endfor %}
so in the end, i get what i wanted, but it seems not very elegant to me