Amit Pal
Amit Pal

Reputation: 11062

How to pass a dictionary to Django-template?

I am not able to get the value of dictionary at Django-template. Take a look on views.py

 def subnet_network(request, page=None):
     ipv4_sub_net , ipv6_sub_net= get_netmask(user=request.user)
     extra_context = {
         'ipv4_net': ipv4_sub_net,
         'ipv6_net': ipv6_sub_net
         }
     return direct_to_template(request, 'networks/subnet_network.html',
                          extra_context=extra_context)

To get the value of these two dictionary and the subnet_network.html. I wrote the following code:

 <tbody>
  {% for k,v in ipv4_net.items %}
     <tr>
         <td>
             {{ v }}
         </td>
    </tr>
  {% endfor %}
 </tbody>

and the same above code is for ipv6_net dictionary While i check my values in dictionary using pdb.set_trace(). It print like this:

 (Pdb) print ipv4_sub_net
 [{'ipv4_sub_net': u'255.0.0.0'}, {'ipv4_sub_net': u'255.255.255.255'}]

 (Pdb) print ipv6_sub_net
 [{'ipv6_sub_net': u'/12'}, {'ipv6_sub_net': u'/128'}]

is there are something wrong with the code?

Upvotes: 0

Views: 1362

Answers (1)

Mikael
Mikael

Reputation: 3234

Looking at the print statement you have a list with 2 dictionaries, not one dictionary with 2 elements. Thus ipv4_sub_net.items is not valid.

If you do this you will see it:

for v in ipv4_sub_net:
    print v

Upvotes: 4

Related Questions