Sending Multiple Dictionary Variables as one in Template

Vaibhav Mishra

I have a dataset in multiple dictionary variables that I want to pass as Template

servera = {'svr': ServerA, 'tc': 10, 'bs': 100, 'bf': 0, 'te': 0, 'per': '0.00'}
serverb = {'svr': ServerB: 'tc': 20, 'bs': 20,  'bf': 0, 'te': 0, 'per': '0.00'}

. .. . and so on around 14 like this

I want to pass them to template as below

def function(request)

#process data using code

return render(request,'output.html'{servera:servera,serverb:serverb,serverc:serverc,.......}

While this works, this increases efforts when adding a dictionary variable, need to add it to the return statement.

How can I achieve below

somevariable = <all dictionary variabled clubbed>


def function(request)

#process data using code

return render(request,'output.html'{somevariabe:somevariable}

and then how will I access that in the Templates

Something like

{% for name in somevariable.list.all %}

{% for servername in name.list.all %}

{{ servername.tc }}

{% endfor %}
{% endfor %}

Any Help will be appreciated

EDIT :

Following is what I am trying my Template File

{% for k in server_stats.all %}

                <tr>
                    <td><p class="small">{{ forloop.counter  }}</p></td>
                    <td><p class="small">{{ k.svr }}</p></td>
                    <td><p class="small">{{ k.te }}</p></td>
                    <td><p class="small">{{ k.bs    }}</p></td>
                    <td><p class="small">{{ k.bf }}</p></td>
                </tr>
{% endfor %}

My Views File Snippet

.
.
..
    elif request.method =='POST' and 'weekly_report' in request.POST :
            generate_backup_report("Weekly")
            report_type ="Weekly"
            rc= {'report_type': report_type}
            for m in mserver_list:
                answer = (getbackup_stats(m))
                print(answer)
                server_stats.append(answer)
                print('Getting Stats for ',m)
            print('These are Server Stats',server_stats)
            return render(request,'viewreport.html',{'mserver_list':mserver_list,'server_stats':server_stats,'tstats':tstats})

The Debug Toolbaar shows that the Variable has transferred successfully to the Template, but cant take values out of it.

'server_stats': [{'bf': 0,
                   'bs': 0,
                   'per': '0.00',
                   'svr': <masterserver: ServerA>,
                   'tc': 0,
                   'te': 0},
                  {'bf': 1,
                   'bs': 4,
                   'per': '-100.00',
                   'svr': <masterserver: Challenger>,
                   'tc': 5,
                   'te': 5},
                  {'bf': 1,
                   'bs': 18,
                   'per': '-100.00',
                   'svr': <masterserver: Serverb>,
                   'tc': 21,
                   'te': 19},
                  {'bf': 0,
                   'bs': 0,
                   'per': '0.00',
                   'svr': <masterserver: ServerC>,
                   'tc': 0,
                   'te': 0},
                  {'bf': 12,
                   'bs': 0,
                   'per': '-1200.00',
                   'svr': <masterserver: ServerD>,
                   'tc': 21,
                   'te': 12},
                  {'bf': 1,
                   'bs': 29,
                   'per': '-100.00',
                   'svr': <masterserver: ServerE>,
                   'tc': 31,
                   'te': 30},
                  {'bf': 30,
                   'bs': 0,
                   'per': '-3000.00',
                   'svr': <masterserver: ServerF>,
                   'tc': 32,
                   'te': 30},
                  {'bf': 0,
                   'bs': 0,
                   'per': '0.00',
                   'svr': <masterserver: ServerG>,
                   'tc': 0,
                   'te': 0},
                  {'bf': 1,
                   'bs': 27,
                   'per': '-100.00',
],
Sammy J

Make a list of server dictionaries and send them to the template and iterate over them in template.Like server_lsit = [servera, serverb, .....] in context send them as ''outpt.html, {'servers': server_list}

In your html template you can iterate like, {% for serv in servers %} {{ serv.svr }}{{ serv.tc }} ... {% endfor %}

Every time the loop runs it is on each server and you can access the data there.This is just one way to do it maybe there is a better way too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related