As you can see I am trying to create a function and use it in the template by calling it in the get_context_data
But when I refresh the page, it gives me the error: name 'sidebar' is not defined.
I think I might need to pass some variables into the sidebarFunction but I am not entirely sure.
HTML:
{% if user.is_staff %}
{% for client in sidebar %}
<li>
<!-- <a href="{% url 'public:client_detail' client.client.pk %}"> -->
<p class="client-title" onclick="subNavDropDown(this)">{{ client.client }}</p>
<!-- </a> -->
</li>
<ul class="sub-nav" id="{{ client.client }}-subnav">
{% for project in client.projects %}
<!-- Add a link to this -->
<li class="sub-nav" id="project-dropdown-{{ project.pk }}">
{{ project }}
</li>
{% endfor %}
</ul>
{% endfor %}
<br>
{% else %}
{% for project in sidebar %}
<li>
<a href="{% url 'public:client_detail' client.pk %}">
<p class="client-title"></p>{{ client }}</p>
</a>
</li>
{% endfor %}
{% endif %}
Python:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
client = self.request.user.groups.all()
context["project"] = Project.objects.filter(client=self.get_object())
context["projects"] = Project.objects.filter(client__in=client, active=True)
context["sidebar"] = sidebar
return context
def sidebarFunction(self):
if self.request.user.is_staff:
sidebar = []
for client in Client.objects.all():
data = {
"client": client,
"projects": Project.objects.filter(client=client),
}
sidebar.append(data)
else:
sidebar = Project.objects.filter(client__in=client, active=True)
asked Aug 7, 2019 at 14:07
Max Loyd
4161 gold badge6 silver badges23 bronze badges
1 Answer 1
What you can do is call the sidebarFunction inside your get_context_data and the values returned from your sidebarFunction will be available in the template.
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
client = self.request.user.groups.all()
context["project"] = Project.objects.filter(client=self.get_object())
context["projects"] = Project.objects.filter(client__in=client, active=True)
context["sidebar"] = self.sidebarFunction()
return context
def sidebarFunction(self):
sidebar = []
data = {}
if self.request.user.is_staff:
for client in Client.objects.all():
data.update({
"client": client,
"projects": Project.objects.filter(client=client),
})
sidebar.append(data)
else:
data.update({'projects': Project.objects.filter(client__in=client, active=True)})
sidebar.append(data)
return sidebar
answered Aug 7, 2019 at 14:13
Higor Rossato
2,0461 gold badge12 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
bruno desthuilliers
the
sidebarFunction() must also be changed to return the sidebar !bruno desthuilliers
@MaxLoyd in your snippet, this functions creates a local variable "sidebar" but doesn't return it to the caller. You have to add "return sidebar" at the end of the function for the whole thing to work.
Higor Rossato
Thanks @brunodesthuilliers. Updated my answer
default
context["sidebar"] = sidebar. What issidebarsupposed to be here?