[Fixed]-Accessing to objects of a dynamic way in django template

1👍

Several possible approaches you can take here. For the all izquierda or all derecha, here is what you can do:

class PatientDetail(LoginRequiredMixin, DetailView):
    model = PatientProfile
    template_name = 'patient_detail.html'
    context_object_name = 'patientdetail'

    def get_context_data(self, **kwargs):
        context=super(PatientDetail, self).get_context_data(**kwargs)
        queryset= RehabilitationSession.objects.filter(patient__slug=self.kwargs['slug'])
        context['patient_session_data'] = queryset
        # get a list of all upper extremities
        upper_extremities = set(queryset.values_list('upper_extremity', flat=True).distinct())
        all_match = dict()
        for value in ('Izquierda', 'Derecha', ): # add any other values you want here
            all_match[value] = value in upper_extremities and len(upper_extremities) == 1
        context['all_match'] = all_match
        return context

And in your template

#if all objects returned have same value in upper_extremity 
{% if all_match.Izquierda or all_match.Derecha %}
   <div class="col-md-10">
      <h5>Segmentos corporales a tratar</h5>
      <small>Codo - mano - falange</small>
   </div>                                           
{%else%}
{# here we handle all cases with mixed results #}
{%endif%}
👤2ps

Leave a comment