python – Concatenate variables in Django template

Question:

Hey guys, good morning, I have a problem. I'm scrolling through a lista but I want to concatenar my list that I'm calling in the template as {{ list }} and {{ forloop.counter0 }} so that my for only displays 1 list item at a time and not all of it

views.py:

def user_donations(request):
    donations = Donation.objects.all().filter(donor_id=request.user.id)

    donationItem = DonationItem.objects.all().filter(donation__donor_id=request.user.id)
    list = []

    for item in donationItem:
        list.append(item.material.name)

    content = {'donations':donations, 'list': list}
    return render(request, 'user-donations.html', content)

html.py:

{% extends 'base.html' %}
{% load static %}

{% block content%}
<section class="user-donations">
    <div class="register-header-desktop">
        <h2>Minhas doações</h2>
    </div>
    <div class="container register">
        <div class="donations-list">
            {% for donation in donations %}
            <div class="modal">
                <div class="confirm-school-name">
                    <span>{{ donation.created_at }}</span>
                    <span>De: {{ donation.donor }}</span>
                    <span>Para: {{ donation.school }}</span>
                </div>
                <div class="list-itens">
                    <span>{{ list.forloop.counter0 }}</span>
                    <span></span>
                </div>
                <div class="confirm-delivery">
                    <span></span>
                </div>
                <div class="arrow">
                    <img src="{% static 'images/arrow-down-icon.png' %}" alt="">
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
</section>
{% endblock %}

But the way I'm trying is not working, can anyone help me?

Answer:

You just iterate the items on the list using a for it will bring one item per iteration without needing a variable for indexing.

For example to create a list in the Django template we can do this:

<div class="list-itens">
    <ul>
        {% for item in list %}
            <li class="item">{{ item }}</li>
        {% endfor %}
    </ul>
</div>
Scroll to Top