Concatenate variables in the Django template

Asked

Viewed 724 times

3

Talk guys, good morning, I have a problem. I’m walking through a lista but I want to concatenar my list I’m calling on template as {{ list }} and {{ forloop.counter0 }} so that in mine is only displayed 1 list item at a time and not all of it

py views.:

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 not to, someone can help me ?

  • You want to spin one for iterating on the items?

  • exact @Thiagoluizs, for example on this {{ list }} I have 3 items, I want to concatenate with the {{ forloop.counter0 }} so that every time my for Rotate when you arrive at the {{ list }} is added a number so that it accesses an index of the vector of list ex: first round{{ list.0 }} second round {{ list.1 }} and so on. Draw ?

2 answers

0

Just iterate the items over the list using a for that it will carry an item per iteration without requiring a variable for indexing.

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

<div class="list-itens">
    <ul>
        {% for item in list %}
            <li class="item">{{ item }}</li>
        {% endfor %}
    </ul>
</div>
  • the problem of me using a for this way is that it returns me the 3 items of the list all at once, in my case, I have 3 donations being that each donation has a single item that is a racket and using a for on the list he will return me the 3 items donated in each donation, it seems that each donor donated 3 rackets

  • 4 de Outubro de 2018 às 18:47&#xA;De: admin@admin&#xA;Para: [email protected]&#xA;Raquete Raquete Raquete&#xA; @Thiagoluizs

  • that’s why I wanted to concatenate my list with a {{ forloop.counter0 }} because that way would solve my problem, "I think skaoskaosko" because when I access the list this way {{ list.0 }} it returns me only the first item of array, and if for every loop I have one i++ do counter maybe solve my problem, man ?

0


To solve the problem I did it this way:

first created a @property:

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

then in the py views.:

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 in the 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 could do what I needed

Browser other questions tagged

You are not signed in. Login or sign up in order to post.