2

I have an array of names in python, which I would like to display using Django templates. I know that tags can be used that support for loops, but they have a unique syntax which I am unfamiliar with.

Also I do not want to use instance.0, instance.1 and so on. I need to be scalable for any size array

Django Template code

(:,

{% for name  in instance  %}
       {{instance}}         
{%endfor%}

 My name is {{my_name}}.
(:,

Python code

from django.template import Template, Context
from django.conf import settings

settings.configure()


t=Template(The above section)
dtype={'names': ['name','offset'],
       'formats':['U20,U20']
       }

instance = np.zeros(5,dtype='U20')
instance[0]=('John')
instance[1]=('Tim')
instance[2]=('Sarah')

name="adrian"


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

Current output

(:,

                ['John' 'Tim' 'Sarah' '' '']


                ['John' 'Tim' 'Sarah' '' '']


                ['John' 'Tim' 'Sarah' '' '']


                ['John' 'Tim' 'Sarah' '' '']


                ['John' 'Tim' 'Sarah' '' '']


       My name is adrian.
       ).

Desired output

(:,
        i=0

               [John]


               [Tim]


               [Sarah]




       My name is adrian.
       ).
1
  • instead of {{instance}}, output {{name}} Commented Oct 21, 2015 at 21:53

1 Answer 1

1

You are outputting the queryset/iterable, i.e. instance.

Instead, you need to output each element of it, which is name, as you correctly state in the for loop.

The syntax of the for is the same as python's (see if this helps)

so just replace

{{instance}}

with

{{name}}

in the template and you should get the desired output

That means the template should be

(:,

{% for name  in instance  %}
       {{name}}
{% endfor %}

 My name is {{my_name}}.
(:,

By the way, I can see the queryset has five elements (the same line was repeated five times because the loop ran on five elements), but the last two lack a name since only John, Tim and Sarah appear.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! Now I have a new use case. How can I have a for loop print out values from 2 different arrays? Like { for name in instance} {{name}} {{Last_name}}
to iterate over two different sequences pairing the elements form each, you can use zip. e.g. {% for name, last_name in zip(arr1, arr2) %} {{ name }} {{ last_name }} {% endfor %}
I don't think it's possible to access zip from within the for loop.All computations have to be done outside the template. Also I think this is my question asked in a better way. stackoverflow.com/questions/33286510/…
Ok, but then how do I use the Context() method with the combined arrays?
I am looking at your new question. Maybe you there is no need to change much. See my comment there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.