Hi everyone, glad to find a forum dedicated to Django
I´m learning Python & Django by working a project: a sports team management app. As of right now, it´s mainly a bunch of forms and tables, so I want to add some “fun” stuff to break this repetitive pattern. In comes graphs that show how the players line up on the field.
I am able to make the graph appear on my ListView, but it´s the same for all items in the list.
Can you please guide me on how to get the context data to be specific to the object ?
ListView:
class FormationList(LoginRequiredMixin, generic.ListView):
model = PersonnelFormation
template_name = 'playbook/formations_list.html'
context_object_name = 'formations'
paginate_by = 8
def get_context_data(self,**kwargs):
context = super(FormationList, self).get_context_data(**kwargs)
plt.switch_backend('agg')
for obj in context['object_list']:
#print(f'pk: {obj.pk}')
form = PersonnelFormation.objects.filter(pk=obj.pk)
pos_list = form.values_list('positions', flat=True)
points_x = []
points_y = []
for k in pos_list:
for v in k:
for i in v:
if i == 'position':
pos = PersonnelRolePosition.objects.get(name__exact=v[i])
points_x.append(pos.x_offset)
points_y.append(pos.y_offset)
plt.scatter(points_x, points_y)
plt.xlabel("X")
plt.ylabel("Y")
fig = plt.gcf()
#convert graph into dtring buffer and then we convert 64 bit code into image
buf = io.BytesIO()
fig.savefig(buf,format='png')
buf.seek(0)
string = base64.b64encode(buf.read())
uri = urllib.parse.quote(string)
context['data'] = uri
return context
def get_queryset(self):
user_filter = self.request.user.pk
return PersonnelFormation.objects.filter(user=user_filter)
Relevant template part:
{% for formation in formations %}
<tr>
<td class="px-4 py-3">{{formation.name}} ({{formation.code_name}})</td>
<td class="px-4 py-3">{{formation.description}}</td>
<td class="px-4 py-3">{{formation.personnel_group}}</td>
<td class="px-4 py-3">
<img src="data:image/png;base64,{{ data }}" alt="" height="250" width="250">
</td>
</tr>
Thanks a lot !