creating one table from two lists in django template

Hi,

I have a view that queries all data in my model (DrinkModel) and passes values to a table. I want to add a column to the table from a function that creates a list. If I pass the second list to the template i need a second iteration and effective just produce a column below the table.

My view:
def d_list(request):
drinks = models.DrinkModel.objects.all()
print(drinks)
duration_qs = duration_queryset()
return render(request, ‘list.html’, {‘drinks’: drinks, ‘duration_qs’: duration_qs})

My template:

{% for drink in drinks %}

    <tr>
        <td>{{ drink.pk }}</td>
        <td><a href="{% url 'detail' drink.id  %}"/> {{ drink.dn }}</td>
        <td>{{ drink.ds }}</td>
        <td>{{ drink.df }}</td>
        <td>{{ drink.dt }}</td>

    </tr>
{% endfor %}

{% for qs in duration_qs %}
    <tr>
        <td>{{ qs }}</td>
    </tr>
{% endfor %}

Is there a way to iterate over duration_qs so that it lines up as the far right column of the first table?

Many thanks,
Brad

Since you didn’t include your definition of duration_queryset it’s difficult to know how to integrate the two.

You’ve got a couple of different ways to handle this.

One way would be to annotate your drinks queryset with the values coming from your duration queryset. That would be my first choice - have the database join the values in the query to give you a single combined list.

You could use the Python map function to create a combined iterable to be passed to the render function.

You could iterate over your drinks list and use the index to refer to the corresponding entry in the duration_qs. See the reference to counter in the template for loop docs