Home > database >  How to iterate over a set of 2 lists and display all lines in HTML?
How to iterate over a set of 2 lists and display all lines in HTML?

Time:05-08

I have a a set of 2 lists like so:

'[52.04061648544843, -0.6655072691644374] -> [52.4967, -1.90425]'
'[52.04061648544843, -0.6655072691644374] -> [52.4967, -1.90425]'
'[54.89272380289834, -2.8951219798364622] -> [57.63161, -3.11041]'

But when i try to render them in html i get only the first line showing in browser:

 {'[52.04061648544843, -0.6655072691644374] -> [52.4967, -1.90425]'}

How can i show all the lines of the lists?

Python file:

def dispatch_data(request):

    for d in d_site_customer_ids:
        data =  '[%s, %s] -> [%s, %s]' % (d['site'].latitude, 
            d['site'].longitude, d['customer'].latitude, d['customer'].longitude)
        
        #print(data)
    
    return render(request, 'dashboard/charts/dispatch.html', {'data':data})

I am rendering the data in html like so but it only returns the first line of the data:

<head>

</head>

<body>
    Hello
    {{ data }}


</body>

Thanks for any help!

SOLUTION:

I FIGURED IT OUT!!

I have to iterate through the lists in my html file like so:

<head>
    <!-- Custom styles for this page -->

</head>

<body>
    Hello
    {% for i in data %}
    {{ data }}
    {% endfor %}


</body>

CodePudding user response:

Loop over the items like this( instead of you for block):

data =  [{'[%s, %s] -> [%s, %s]' % (d['site'].latitude, d['site'].longitude, d['customer'].latitude, d['customer'].longitude)} for d in d_site_customer_ids]

the problem in your code is that in each iteration of your loop you are replaceing previous data with the current data (data = ...).

the key is to use list of keep all of your dictionaries, other wise you should create a custom dictionary to keep all of them in seprated keys. but I strongly recommend you to use list for this purpose.

  • Related