Concatenating variables in the Django template

3

Talk to people, good morning, I have a problem. I'm running a lista but I want concatenar my list that I'm calling in template as {{ list }} and {{ forloop.counter0 }} so that in my is only displayed 1 list item at time and not her entire

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?

    
asked by anonymous 05.10.2018 / 17:05

2 answers

0

To solve the problem I did as follows:

1st I created a @property :

@property def items(self): return self.donationitem_set.all()

then views.py:

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

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

and user-donations.html:

{% 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>
                {% for item in donation.items %}
                <div class="list-itens">
                    <span>{{item.quantity}} {{item}}</span>
                </div>
                {% endfor %}
                <div class="confirm-delivery">
                    <span></span>
                </div>
                <div class="arrow">
                    <img src="{% static 'images/arrow-down-icon.png' %}" alt="">
                </div>
            </div>
            {% endfor %}
        </div>
    </div>
    </div>
</section>
{% endblock %}

That way I was able to do what I needed.

    
09.10.2018 / 18:30
0

Simply you iterate the items on the list using a for that it will take care of bringing an item by iteration without needing a variable for indexing.

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

<div class="list-itens">
    <ul>
        {% for item in list %}
            <li class="item">{{ item }}</li>
        {% endfor %}
    </ul>
</div>
    
05.10.2018 / 19:42