Iterating over 2 arrays or 1 record array in Django

217 views
Skip to first unread message

Juan Pablo Rivera

unread,
Oct 22, 2015, 4:05:27 PM10/22/15
to Django users

I have a numpy record array with 2 values per slot in the array.The name and offset, I want to display both of these values side by side using a Django template.

Python code

import numpy as np
from django.template import Template, Context, loader
from django.conf import settings

dtype={
    'names' : ('name','offset'),
    'formats' : ('U20','U20')}

instance= np.zeros(3,dtype)


instance[0]=('xga_control_reg','008')
instance[1]=('i_cmd_REG','012')
instance[2]=('i_ee_cmd_reg','016')

t=Template(The Django Template below)
c=Context({"instance":instance['name'],"instance":instance['offset']})
print(t.render(c))

Django Template

--
--   generated with parser version 1.09
--
library ieee;
use ieee.std_logic_1164.all;

package regfile
          (
            {% for name  in instance%}
                   Name is  {{name}}  "{{name}}" 
            {%endfor%}

           ).""")

Current output

--
--   generated with parser version 1.09
--
library ieee;
use ieee.std_logic_1164.all;


package regfile
          (

               Name is  008  "008" 

               Name is  012  "012" 

               Name is  016  "016" 


          ).

Desired Output

--
--   generated with parser version 1.09
--
library ieee;
use ieee.std_logic_1164.all;


package regfile
      (

               Name is  xga_control_reg  "008" 

               Name is  i_cmd_REG  "012" 

               Name is  i_ee_cmd_reg  "016" 

       ).

I need to be able to display 2 values on the same line using the tag for loop in a Django template. If this can be done with 2 separate arrays instead of a numpy record array, that would be acceptable. Thank you!

C. Kirby

unread,
Oct 23, 2015, 11:56:58 AM10/23/15
to Django users
When you have duplicate keys in a python dict only the second gets saved. You can also refer to positions in tuples with . notation in templates. So something like:

Python code:
import numpy as np
from django.template import Template, Context, loader
from django.conf import settings

dtype={
    'names' : ('name','offset'),
    'formats' : ('U20','U20')}

instance= np.zeros(3,dtype)


instance[0]=('xga_control_reg','008')
instance[1]=('i_cmd_REG','012')
instance[2]=('i_ee_cmd_reg','016')

t=Template(The Django Template below)

c=Context({"instance":instance})
print(t.render(c))

template:

--
--   generated with parser version 1.09
--
library ieee;
use ieee.std_logic_1164.all;

package regfile
          (

            {% for i in instance%}
                   Name is  {{i.0}}  "{{i.1}}" 
            {%endfor%}

           ).""")

Reply all
Reply to author
Forward
0 new messages