Loop in template - just the last element appears

Hi!

I have two loops in my view:

for u in Performance.objects.raw('SELECT * FROM...')
    name = u.last_name + ' ' + u.first_name

    for e in Projekt_perf.objects.raw('SELECT stressz_profile.id...')
        total = e.total

        results = {'name':name, 'total':total}

I need to use the name from the first loop in the second loop and it works fine, gives me a list.
like this:

{'name': 'Someone01', 'total': 25}
{'name': 'Someone02', 'total': 7}
{'name': 'Someone03', 'total': 10}
{'name': 'Someone04', 'total': 0}

I like to use this list in my template but it give me just the last result.
I tried these methods:

{% for r in results %}
  {{ r }}
{% endfor %}

{% for r in results %}
  {{ r.name }} - {{ r.total }}
{% endfor %}

Thank you in advance!

We’re going to need to see the complete view in order to perform an accurate diagnosis.

What we can see from what you have posted is that you’re only creating 1 dict in your inner loop, so when the loops exit, results will have the last dict that was created.

(This isn’t a template problem, the problem is likely in your view.)

Hi Ken!
Thank you for the reply. Here is the complete view (sorry but the second query is very long):

def list(request, projekt_perf_id, projekt_perf):

    for u in Performance.objects.raw('SELECT * FROM performance_performance_profile INNER JOIN stressz_profile ON stressz_profile.user_id = performance_performance_profile.user_name_id LEFT JOIN auth_user ON stressz_profile.user_id = auth_user.id WHERE stressz_profile.projekt_perf_id = %s', [projekt_perf_id]):
        name = u.last_name + ' ' + u.first_name
        
        for e in Projekt_perf.objects.raw('SELECT *, FLOOR(TOTAL(perf01a = %s) + TOTAL(perf01b = %s) + TOTAL(perf01c = %s)) AS total FROM performance_performance_profile INNER JOIN auth_user ON auth_user.id = performance_performance_profile.user_name_id INNER JOIN performance_performance ON performance_performance.user_name_id = stressz_profile.user_id INNER JOIN stressz_profile ON stressz_profile.user_id = performance_performance_profile.user_name_id INNER JOIN performance_projekt_perf ON performance_projekt_perf.id = performance_performance_profile.projekt_perf_id INNER JOIN stressz_csoportok ON stressz_csoportok.id = stressz_profile.csoport_id WHERE %s IN (perf01a, perf01b, perf01c) AND performance_projekt_perf.id = %s AND projekt_perf = %s AND stressz_profile.performance = 1 ORDER BY stressz_profile.csoport_id, auth_user.last_name',[name, name, name, name, projekt_perf_id, projekt_perf]):
            total = lastname, e.total, 
            
            results = {'name':name, 'total':total}
            print(results)

I don’t see how/where you’re rendering results from here. Nor do I see where you’re returning anything from this view. (A view is supposed to return a Response object.)

(Also, you could probably shorten those queries by writing them using the orm instead of as raw queries.)

Sorry I forgot this part:

context = {
        'results': results,
        'name': name,
        'total': total,    
    }
    
    return render(request, 'performance/list.html', context)

So yes, you’re only creating one “results” dictionary.

From the looks of your template, what it appears like you’re looking for is to define results as a list, and then append each of those dictionaries to that list as they’re created.

1 Like