0

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
1
  • 1
    look at the line context["sidebar"] = sidebar. What is sidebar supposed to be here? Commented Aug 7, 2019 at 14:14

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

3 Comments

the sidebarFunction() must also be changed to return the sidebar !
@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.
Thanks @brunodesthuilliers. Updated my answer

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.