Reputation: 83
I'm building a Django app that needs two things. First, it needs the set of groups that the user has access to. With this set of groups, my template is supposed to create a button for each group. Next, it needs the set of "Property" objects that are ManyToMany related to a group in the previously found set of groups (this is to say that I need every property that is assigned to a group that the user has access to). For each of these properties, a button is created. I'm working with an admin user on localhost right now while building this, and I've created a test group with test properties and assigned the admin user to that group. Django however, doesn't seem to be detecting this. The relevant code is below:
views.py
from __future__ import unicode_literals
from django.shortcuts import render
from UsageData.models import Property, User, Group, MeterData
from django.contrib import auth
def reports_proerty_list(request):
if request.user.is_authenticated():
current_user_groups = Group.objects.filter(id__in=request.user.groups.all())
current_user_properties = Property.objects.filter(groups__in=current_user_groups)
return render(request, 'App/App-Template-Page.html')
else:
# The user is redirected to the sign-in page
App-Template-Page.html
<div class="row">
{% if current_user_groups %}
{% for group in current_user_groups %}
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">{{ group.name }}</button>
</div>
{% endfor %}
{% else %}
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 1</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 2</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 3</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 4</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 5</button>
</div>
<div class="col-sm-2 col-md-2 col-lg-2 text-center">
<button class="tab-selector inactive" id="{{ group.id }}">Group 6</button>
</div>
{% endif %}
</div>
The problem I'm having is that the {% else %} part of the template displays when the page loads, which I suppose means that current_user_groups doesn't exist. Anyone know why this might be happening? I naturally can't proceed with what I want to do with the Property objects without this working first. Thanks!
Upvotes: 1
Views: 62
Reputation: 15738
You haven't pass any of your variables into context of a render function
return render(request, 'App/App-Template-Page.html',{
'current_user_groups': current_user_groups,
'current_user_properties': current_user_properties
})
Also this line could be simplified
current_user_groups = Group.objects.filter(id__in=request.user.groups.all())
to
current_user_groups = request.user.groups.all()
Upvotes: 2