Show only the first element of a list in templates


I have a table in which I show an object of sight, these are vaccines, what I want is that, for example, if they already put that vaccine, I will not get the name of the vaccine, only the dates in those who got the vaccines.


<table width="100%" class="table-bordered">
  <caption style="text-align: center; font-weight:bolder; font-size: 20px; color:black">Vacunas aplicadas</caption>
    <td>Fecha de aplicación</td>
{% for vaccine in preventive %}
        <td>{{ forloop.counter }}</td>
        <td>{{ vaccine.vaccines }}</td>
        <td>{{ vaccine.vaccine_date }}</td>
        <td>{{ vaccine.vaccine_note}}</td>
        {% ifchanged vaccine.vaccines%}
{% endfor %}

class PreventiveMedicineView(FormView):
    template_name = 'clipboard_medicinapreventiva.html'
    form_class = PrevMedicineForm

    def form_valid(self, form, **kwargs):
        vac = self.request.POST['description']
        vaccines = Vaccines.objects.get(description=vac)
        vaccine_date = self.request.POST['vaccine_date']
        vaccine_note = self.request.POST['vaccine_note']

        if not vaccine_date:

        clipboard_patient = Clipboard.objects.get(patient=patient)

        prev_medicine = PreventiveMedicine(clipboard=clipboard_patient,
        success_url = reverse('preventive_medicine', kwargs={'id':} )
        return HttpResponseRedirect(success_url)

    def form_invalid(self, form, **kwargs):
        print form._errors
        return self.render_to_response(self.get_context_data(form=form))

    def get_context_data(self, **kwargs):
        context = super(PreventiveMedicineView, self).get_context_data(**kwargs)
            'patient': Patient.objects.get(pk=self.kwargs['id']),
            'prev_medicine': PreventiveMedicine.objects.all(),
            'vaccines': Vaccines.objects.all(),
            'preventive': PreventiveMedicine.objects.filter(clipboard__patient=self.kwargs['id'])
        return context

For example this form shows them to me like this:



H1N1   --2016-09-11--observaciones


Well, as I said above, what I want is that in the previous example, I'll show it to me like this




H1N1   --2016-09-11--observaciones

asked by Mac Alexander 09.11.2016 в 20:01

2 answers


It is not clear to me what is the criterion to know if they have already given a vaccine or not, but in any case, my advice is to define it either in the view or in an Object Manager of the vaccine model and check the fulfillment of this criterion with a {% if%} in the template. For example, if there is a "set" attribute that is a Vaccine boolean, then you can say in the template:

{% for vaccine in preventive %}
    <td>{{ forloop.counter }}</td>
        {% if not vaccine.puesta %}
            {{ vaccine.vaccines }}
        {% endif %}
    <td>{{ vaccine.vaccine_date }}</td>
    <td>{{ vaccine.vaccine_note}}</td>
    {% ifchanged vaccine.vaccines%}
{% endfor %}

I hope I helped you. Regards!     Erick

answered by 24.01.2017 в 17:49

I can think of a simple solution using property as follows:

First, suppose you have the following two models:

class Vacuna(models.Model):
    nombre = models.CharField(max_length=30)

    def obtener_datos(self):
        return DatosVacuna.objects.filter(vacuna_id = self)

    datos = property(obtener_datos) # Atributo property, obtengo todas las fechas y descripciones correspondientes a cada vacuna

class DatosVacuna(models.Model):
    vacuna_id = models.ForeignKey(Vacuna, on_delete=models.CASCADE)
    descripcion = models.CharField(max_length=30)
    fecha_puesta = models.DateField()

Then later in the template you could make use of that property attribute in the following way:

{% for vacuna in vacunas %}
        <td>{{ vacuna.nombre }}</td>
        {% for dato in vacuna.datos %}
            <td>--{{ dato.fecha_puesta }}--{{dato.descripcion}}</td>
        {% endfor %}
{% endfor %}

In this way you go over all the dates and descriptions corresponding to each vaccine. In addition, since the database model is standardized, you will not have problems with the names of the vaccines, and in this way you avoid problems when changing the name of a vaccine or writing it incorrectly.

I hope I have been of help.

answered by 28.03.2017 в 23:16