0

i'm new on Django, and I would like to know how can I create a kind of filter so that only an object appears if it meets a condition in one of its attributes. I tried to do it with tags in the template but it did not work and I don't know if it is the most efficient way. The case is for example that I would like to see in my table the objects that in their attribute "asignar" have a specific value for example the text "Pedro". I'm open to suggestions that do not necessarily apply with labels.

Models.py:

class reporte_fallo(models.Model):
    num_inventario = models.CharField(max_length=25)
    serie = models.CharField(max_length=25)
    falla = models.TextField()
    descripcion = models.TextField()

    LOAN_STATUS = (
        ('m', 'Maintenance'),
        ('o', 'On loan'),
        ('a', 'Available'),
        ('r', 'Reserved'),
    )

    area = models.CharField(max_length=1,choices=LOAN_STATUS,blank=True)
    fecha = models.DateField(auto_now=False, auto_now_add=True)
    asignar = models.CharField(max_length=20, default=True)
    fecha_comp = models.DateField(default=timezone.now)

View.py:

class reportelist(ListView):
    model = reporte_fallo
    template_name = 'formulario/reporte_list_jefe.html'

Template.html (This is my template that did not work using tags in this case the part "and report.assign == "Pedro" on the if, it just send the message "The list is empty")

    <tbody>
    {% if object_list and reporte.asignar == "Pedro" %} 
    {% for reporte in object_list %}
    <tr>
        <td>{{reporte.id}}</td>
        <td></td>
        <td>{{reporte.falla}}</td>
        <td>{{reporte.fecha}}</td>
        <td>{{reporte.area}}</td>
        <td>{{reporte.asignar}}</td>
        <td>{{reporte.fecha_comp}}</td>
        <td></td>
        <td>
        <a href="{% url 'formulario:reporte_editar' reporte.id %}">Asignar</a>
        <a href="{% url 'formulario:reporte_eliminar' reporte.id %}">Eliminar</a>       
        </td>
    </tr>
    {% endfor %}
    {% else %}
    <h1>The list is empty</h1>
    {% endif%}
</tbody>

I hope your answer thnks a lot!

1 Answer 1

2

In the template the if conditional should be moved inside the {% for %} loop. Something along the lines of:

{% for reporte in object_list %}
{% if reporte.asignar == "Pedro" %} 
    <tr>
        <td>{{reporte.id}}</td>
    </tr>
{% endif %}
{% endfor %}

If you only want to see reporte.asignar == "Pedro" objects it might be worth overriding the queryset in the view:

# views.py
class reportelist(ListView):
    model = reporte_fallo
    template_name = 'formulario/reporte_list_jefe.html'

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(asignar='Pedro')

Then there's no need to check in the template:

{% for reporte in object_list %}
    <tr>
        <td>{{reporte.id}}</td>
    </tr>
{% endfor %}
1
  • Thanks a lot!!! You are huge, my problem was that i didn't know what could I do by overwriting a view, but thank you.
    – Xultron
    Feb 28, 2019 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.